Python内置函数globals_locals 的使用
"""本文件:研究内置函数:globals locals"""a = 1b = 2def func(): name = ‘alex‘age = 73print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。print(locals()) # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。# print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。# print(locals()) # # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。func()
以下是打印结果:
{‘__name__‘: ‘__main__‘, ‘__doc__‘: ‘\n本文件:研究内置函数:globals locals\n‘, ‘__package__‘: None, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x0000020A35DCC0B8>, ‘__spec__‘: None, ‘__annotations__‘: {}, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__file__‘: ‘D:/python/lanzou/day/day10/05globals_locals.py‘, ‘__cached__‘: None, ‘a‘: 1, ‘b‘: 2, ‘func‘: <function func at 0x0000020A35CC2F28>}
{‘age‘: 73, ‘name‘: ‘alex‘}
def func(): name = ‘alex‘age = 73print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。print(locals()) # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。# print(globals()) # 返回的是字典:字典里面的键值对:全局作用域的所有内容。# print(locals()) # # 返回的是字典:字典里面的键值对:当前作用域的所有的内容。func()
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
世事一场大梦 2020-11-17
xiaoseyihe 2020-11-16
Morelia 2020-11-03
CloudXli 2020-11-03
文山羊 2020-10-31
comtop0 2020-10-31
pythonxuexi 2020-10-30
三石 2020-10-29
chaochao 2020-10-27
PythonMaker 2020-10-27