1, global variables, local variables
Global variables are not allowed to be modified directly in the calling function .
Modification method: add the global keyword before the global variable
Such as: modify the global variable text, method: global text="XXX"
For the reason that the local variable is modified in the calling function, but the output local variable is still the original value:
It is the function that creates a local variable with the same name as the global variable in the stack, and the value acts on the function that currently modifies it
2. Internal nested functions
Inner nested functions: functions inside functions
The scope of the inner function is only valid in the parent function. The variables defined in the parent function and outside the child function are equivalent to global variables for the child function, so the "global variable" of the child function cannot be directly modified. The modification method is as follows:
1) Use the data of the container type to modify the data of the container type
def fun11(): x=[6] def fun22(): x[0] *= x[0] return x[0] return fun22()
2), add the nonlocal keyword
def fun111(): x=7 def fun222(): nonlocal x x *= x return x return fun222()