发布时间:2019-07-30 10:28:30编辑:auto阅读(1782)
python使用命名空间记录变量。python中的命名空间就像是一个dict,key是变量的名字,value是变量的值。
python中,每个函数都有一个自己的命名空间,叫做local namespace,它记录了函数的变量。python中,每个module有一个自己的命名空间,叫做global namespace,它记录了module的变量,包括 functions, classes 和其它imported modules,还有 module级别的 变量和常量。build-in 命名空间,可以被任意模块访问,这个build-in命名空间中包含了build-in function 和 exceptions。当python中的某段代码要访问一个变量x时,python会在所有的命名空间中寻找这个变量,查找的顺序为:
namespace中都找不到变量x,python会假设x是build-in的函数或者变量。如果x不是内置函数或者变量,python会报错NameError。对于闭包来说,这里有一点区别,如果在local namespace中找不到变量的话,还会去父函数的local namespace中找变量。
内置函数locals(), 返回当前函数(方法)的局部命名空间
def func(a = 1):
b = 2
print(locals())
return a+b
func()
# {'a': 1, 'b': 2} 可以看出,locals返回的是个dict
内置函数globals(),返回当前module的命名空间
def func(a = 1):
b = 2
return a+b
func()
print(globals()) # globals()返回的也是个dict
locals()和globals()有一个区别是,locals只读,globals可以写
def func(a = 1):
b = 2
return a+b
func()
glos = globals()
glos['new_variable'] = 3
print(new_variable)
# 3 , 我们并没有显示定义new_variable这个变量,只是在globals中添加了这个key,在随后的代码中,
#就可以像访问一般变量一样来访问。
def func(a = 1):
b = 2
locs = locals()
locs['c'] = 1
print(c)
func()
# NameError: name 'c' is not defined
import module时,module本身被引入,但是保存它原有的命名空间,所以我们需要使用module.name这种方式访问它的 函数和变量。from module import这种方式,是将其它模块的函数或者变量引到当前的命名空间中,所以就不需要使用module.name这种方式访问其它的模块的方法了。python中的module也是对象,所有的modules都有一个内置的属性__name__,模块的__name__属性的值取决于如何使用这个模块,如果import module,那么__name__属性的值是模块的名字。如果直接执行这个模块的话,那么__name__属性的值就是默认值__main__。
上一篇: Python插入数据到elasticse
下一篇: sublime text 3 Pyth
51272
50713
41317
38129
32591
29500
28351
23215
23184
21507
1584°
2304°
1912°
1854°
2174°
1897°
2587°
4325°
4176°
2980°