python的namespace的理解


 
python中的名稱空間是名稱(標識符)到對象的映射。
具體來說,python為模塊、函數、類、對象保存一個字典(__dict__),里面就是重名稱到對象的映射。
-------------------------------------------------------------------------------------------
import urllib
import re
x=1 # 變量
def abc(): # 函數
pass
def qq(self): # 方法
pass
class typ(object): # 類
"""docstring for typ"""
def __init__(self, arg):
super(typ, self).__init__()
self.arg = arg
def classqq(self): # 不存在於全局變量中
pass
 
print(globals().keys()) # 打印字典中的key值
print()
print(globals()) # 打印全局變量,打印出來是以字典的形式展示
-----------------------------------------------------------------------------------------------
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'x', 'abc', 'qq', 'typ'])
 
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000000001DEC048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:\\Learn\\practice\\case1.py', '__cached__': None, 'urllib': <module 'urllib' from 'D:\\Programs\\Python\\Python36\\lib\\urllib\\__init__.py'>, 're': <module 're' from 'D:\\Programs\\Python\\Python36\\lib\\re.py'>, 'x': 1, 'abc': <function abc at 0x00000000001F2E18>, 'qq': <function qq at 0x00000000021FAAE8>, 'typ': <class '__main__.typ'>}
-------------------------------------------------------------------------------------------
 
 
x=1 # 變量
def abc(): # 函數
pass
def qq(self): # 方法
pass
class Typ(object): # 類
"""docstring for typ"""
k=1 # 私有變量沒有被init初始化
def __init__(self):
super(Typ, self).__init__()
self.y = 2
self.z = 3
def func(self): # 函數方法不存在於全局命名空間中
print("abcd") # 函數方法會默認return None
func.fx = 2
 
test1 = Typ()
print(Typ.__dict__)
print(test1.__dict__)
print(test1.func.__dict__)
print(globals().keys())
----------------------------------------------------------------------------------------------
{'__module__': '__main__', '__doc__': 'docstring for typ', 'k': 1, '__init__': <function Typ.__init__ at 0x00000000029007B8>, 'func': <function Typ.func at 0x0000000002900840>, '__dict__': <attribute '__dict__' of 'Typ' objects>, '__weakref__': <attribute '__weakref__' of 'Typ' objects>}
{'y': 2, 'z': 3}
{'fx': 2}
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'x', 'abc', 'qq', 'Typ', 'test1'])
[Finished in 0.1s]
 
locals
內置函數 locals(), 返回當前函數(方法)的局部命名空間
def function(a=1):
b=2
print(locals())
return a+b
print(function())
-----------------------------
{'b': 2, 'a': 1}
3
globals
內置函數 globals(),返回當前module的命名空間
def function(a=1):
b=2
print(locals())
return a+b
print(function())
print(globals().keys())
--------------------------------------
{'b': 2, 'a': 1}
3
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'function'])
[Finished in 0.1s]
 
locals()和globals()有一個區別是,locals只讀,globals可以寫
 
from module import 和 import module
  • 使用import module時,module本身被引入,但是保存它原有的命名空間,所以我們需要使用module.name這種方式訪問它的 函數和變量。
  • from module import這種方式,是將其它模塊的函數或者變量引到當前的命名空間中,所以就不需要使用module.name這種方式訪問其它的模塊的方法了。
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM