Introduction to Python function scopes

Source: Internet
Author: User
1. Definition: Where the variables in the function are valued;
2. The variable name in a function is a local variable except that it is declared as a global variable or a local variable;
3. The scope resolution principle of variables: the LEGB principle, namely:
Variable name introduction is divided into three scopes to find, first local, then inside the function (if present), then the global variable, and finally the built-in scope (built-in scope: The module of the built-in function);
? By default, variable name assignment creates or alters local existing variables;
? A global or non-local variable declaration maps an assignment variable name to the internal scope of a module file;
LEGB mechanism:
? The four scopes of the Python search are first the local scope (L), followed by the scope of the DEF in the previous layer (E, function nesting), the global scope (G), and finally the built-in scope (B)
? Python will stop at the first place to find the variable name, and if Python does not find the variable in this search, it will get an error; (that is, undefined)

Introduction to Python function scopes

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.