Python built-in function (--locals)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.