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这种方式访问其它的模块的方法了。
 
 
原文地址:https://www.cnblogs.com/TomBombadil/p/10979588.html