python的namespace的理解,pythonnamespace,Python命名空间


Python命名空间的本质python中的名称空间是名称(标识符)到对象的映射。具体来说,python为模块、函数、类、对象保存一个字典(__dict__),里面就是重名称到对象的映射。-------------------------------------------------------------------------------------------import urllibimport rex=1 # 变量def abc(): # 函数passdef qq(self): # 方法passclass typ(object): # 类"""docstring for typ"""def __init__(self, arg):super(typ, self).__init__()self.arg = argdef classqq(self): # 不存在于全局变量中passprint(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(): # 函数passdef qq(self): # 方法passclass Typ(object): # 类"""docstring for typ"""k=1 # 私有变量没有被init初始化def __init__(self):super(Typ, self).__init__()self.y = 2self.z = 3def func(self): # 函数方法不存在于全局命名空间中print("abcd") # 函数方法会默认return Nonefunc.fx = 2test1 = 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]https://blog.csdn.net/u012436149/article/details/72819539locals内置函数locals(), 返回当前函数(方法)的局部命名空间def function(a=1):b=2print(locals())return a+bprint(function())-----------------------------{‘b‘: 2, ‘a‘: 1}3globals内置函数globals(),返回当前module的命名空间def function(a=1):b=2print(locals())return a+bprint(function())print(globals().keys())--------------------------------------{‘b‘: 2, ‘a‘: 1}3dict_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这种方式访问其它的模块的方法了。

python的namespace的理解

评论关闭