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