阅读背景:

Python中的locals()函数和globals()函数

来源:互联网 
name = 'Tom'
age = 18


def func(x, y):
    sum = x + y
    _G = globals()
    _L = locals()
    print(id(_G), type(_G), _G)
    print(id(_L), type(_L), _L)


func(10, 20)

# 输出结果:
# 140622020688624 <class 'dict'> {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fe51d92dc90>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/sunjiakuo/PycharmProjects/mytest/test4.py', '__cached__': None, 'name': 'Tom', 'age': 18, 'func': <function func at 0x7fe51d8aa050>}
# 140622020418608 <class 'dict'> {'x': 10, 'y': 20, 'sum': 30, '_G': {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fe51d92dc90>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/sunjiakuo/PycharmProjects/mytest/test4.py', '__cached__': None, 'name': 'Tom', 'age': 18, 'func': <function func at 0x7fe51d8aa050>}}name = 'Tom'
age = 18


def func(x, y):
 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: