Python3 advanced --- variable scope

1. Local scope (Local)

   (1) The innermost part contains local variables, such as def/class/lambda. The following sum is a local variable, and the scope is below the add function

   (2) The local scope can refer to global variables, but cannot be modified. If you need to modify it, you need to use the global keyword to declare

   

2. Nested scope (Enclosing)

   Contains non-local and non-global variables. For example, if the B function is nested under the A function, then for the name in B, the scope in A is nonlocal

3. Global scope (Global)

   Contains all variables of the current module

4. Built-in scope (Built-in)

   Contains built-in variables/keywords, etc.

 

Rule order: from inner and outer local scope (L)-"nested scope (E)-"global scope (G)-"built-in scope (B)

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_19982677/article/details/108165945