English documents:
locals
()
Update and return a dictionary representing the current local symbol Table. Free variables locals()
was returned by when it was called in function blocks and not in class Blocks.
Description
1. Function function returns a dictionary of local variables and their values in the current scope, similar to the GLOBALS function (returns a global Variable)
>>>locals () {'__package__': None,'__loader__': <class '_frozen_importlib. Builtinimporter','__doc__': None,'__name__':'__main__','__builtins__': <module'Builtins'(built-inch);'__spec__': None}>>> a = 1>>> locals ()#one more key for a value of 1{'__package__': None,'__loader__': <class '_frozen_importlib. Builtinimporter','a': 1,'__doc__': None,'__name__':'__main__','__builtins__': <module'Builtins'(built-inch);'__spec__': None}
2. can be used in Functions.
>>>defF ():Print('before define a') Print(locals ())#No variables in scopeA = 1Print('after Define a') Print(locals ())#There is a variable a in scope with a value of 1>>>F<function F at 0x03d40588>>>>F () before define a {} after define a{'a': 1}
3. The returned dictionary collection cannot be Modified.
>>>defF ():Print('before define a') Print(locals ())#No variables in scopeA = 1Print('after Define a') Print(locals ())#There is a variable a in scope with a value of 1b =Locals ()Print('b["a"]:', b['a']) b['a'] = 2#Modify the value of b[' a '] Print('change Locals value') Print('b["a"]:', b['a']) Print('a is'A#the value of a is not changed>>>F () before define a {}after define a{'a': 1}b["a"]: 1change Locals valueb["a"]: 2a is1>>>
Python built-in function (--locals)