• locals() -局部變量的字典
• globals() -全局變量的字典
def test_fun():
a=2
print(locals())
print(globals())
test_fun()
輸出:
{
'a': 2}
{
'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f4759f254c0>, '__spec__': None, '__annotations__': {
}, '__builtins__': <module 'builtins' (built-in)>,....,'test_fun': <function test_fun at 0x7f4759ee21f0>}