In Python, the scope of a variable is always determined by where the value is assigned in the code.
- The function defines the local scope, and the module defines the global scope.
If you want to define global scopes within functions, you need to add the global modifier.
- Variable name resolution: LEGB principle
When using an unauthenticated variable name in a function, Python searches for 4 scopes [local scope (L) (a variable inside the function declares but does not use global), followed by the local scope (E) of def or lambda in the previous layer, followed by the global scope (G) (a variable declared in a function using global or a variable declared at the module layer), finally a built-in scope (B) (that is, Python's built-in classes and functions, etc.)] and stops at the first place where the variable name can be found. If the variable name is in the entire search process
Are not found, Python will error.
Complement: The above variable rules apply only to simple objects, and when the properties of a referenced object appear, there is another set of search rules: Property references Search One or more objects, not scopes, and may involve so-called "inheritance"
- Above based on http://blog.csdn.net/carolzhang8406/article/details/6855525
- The following is a discussion of the use of the global modifier:
- The first is a singular phenomenon of pythond, a variable defined at the module level (no global modification is required), and if the variable with the same name is not defined in the function, it can be used as a global variable in the function:
Hehe=6def f (): print(hehe) f ()
The above code works correctly and outputs 6 and 6
- However, if there is a re-assignment/definition in the function (because Python is a weakly typed language, and the assignment statement is the same as the statement that defines the variable), an error is generated that references the undefined variable:
Hehe=6def f (): print(hehe) hehe=2f ()
The error message that is thrown is:
- unboundlocalerror:local variable ' hehe ' referenced before assignment
And if the definition in the function is used before the reference, then the variables in the function and the global variables defined in the module are not the same
Hehe=6def f (): hehe=2 print(hehe) f ()
The above output is 2 and 6, also known as the F function, where print uses the local variable hehe, and the last print statement uses the global hehe.
- Then we will have doubts, if I might use a variable after the function to modify it (and then assign a value), how to let the variables used in the function is the module layer defined by the global variable instead of the inside of the function of the local variables? This is where the global modifier comes in handy.
Hehe=6def f (): global hehe print(hehe) hehe=3f ()
After declaring hehe as a global variable with the global modifier (note that the global statement does not allow simultaneous assignment such as global hehe=3 is not allowed), the above output is 6 and 3, which gives us the desired effect.
Scope and global usage in Python