生活随笔 
收集整理的這篇文章主要介紹了
                                
Python命名空间 
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            命名空間namespace  定義 
 內建命名空間(Builtins):在啟動python解釋器時創建,我覺得是特殊的全局命名空間,因為它相當于導入了builtins 模塊 全局命名空間(Global):在導入模塊時創建 局部命名空間(Local):在調用函數時創建 內嵌函數命名空間(Enclosing):函數內嵌函數時的特殊局部命名空間 創建 
 Python 3.7.0 (default, Jun 28 2018, 07:39:16) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func1():
...     name = 1
...     print('localspace:',locals())
...     print('globalspace:',globals())
...     def func2():
...             print('before func1 local:',locals())
...             print('before func1 global:',globals())
...             ayu = name
...             print('after func1 local:',locals()) #1
...             print('after func1 global:',globals())
...     func2() #2
...     print('after func2 local:',locals())
...     print('after func2 global:',globals())
... 
>>> func1()
localspace: {'name': 1}
globalspace: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'func1': <function func1 at 0x10384c598>}
before func1 local: {'name': 1}
before func1 global: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'func1': <function func1 at 0x10384c598>}
after func1 local: {'name': 1, 'ayu': 1}
after func1 global: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'func1': <function func1 at 0x10384c598>}
after func2 local: {'name': 1, 'func2': <function func1.<locals>.func2 at 0x103855378>}
after func2 global: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'func1': <function func1 at 0x10384c598>}
>>> del(func1) #3
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> 
 
以上代碼在命令行中執行,#2中調用完func2之后,#1中的局部命名空間被刪除,執行#3之后,func1中的全局命名空間被刪除,而builtins命名空間需要在退出解釋器后才會被回收
 調用 
 >>> def func1():
...     x = 1
...     print(x)
...     def func2():
...             x += x
...             print(x)
...     func2()
... 
>>> func1()
1
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 7, in func1File "<stdin>", line 5, in func2
UnboundLocalError: local variable 'x' referenced before assignment
>>>  
在執行func2時會報錯,因為執行func2時,會在局部變量中創建x,然后從其局部變量中查找x,始終沒有到Enclosing中取。
  參考的博客
  由一個例子到python的名字空間
  
 
轉載于:https://blog.51cto.com/dorebmoon/2336954
                            總結 
                            
                                以上是生活随笔 為你收集整理的Python命名空间 的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。