Python global variables and local variables-function parameter transfer attention

```

# The variable defined outside the function is a global variable, the scope is global, and the variable defined in the function is local, and the scope is only inside the function 
# The global variable can be accessed
in the function , if the function is defined with the global variable Variables with the same name, then these two variables # have the same name, but they are not the same variable. When accessing this variable in the function, the variable in the function is used. The global variable can be accessed in the function, but can it be modified?
# Division two Cases: 1. If the global variable is a variable type, such as a list, dictionary, etc., it can be modified in the function. If it is an immutable type, such as a string, then it cannot be directly
modified
in the function. # Modify, you need to add the global key The word declares that this variable is a global variable # The parameter transfer of the python function is value transfer, and the actual parameter variable transfers the value to the formal parameter reception. In theory, if the value of the formal parameter is modified, it will not affect the external actual parameter variable.
# But it needs to be divided into situations: if the actual parameter variable is a variable type, although it is a value transfer, the formal parameter refers to the value of the actual parameter variable, so in this case, after the formal parameter is modified, the external actual parameter variable will also be Following the change,
# But if the parameter is re-assigned, it will not affect the actual parameter variable
# If it is an immutable type, modifying the parameter will not affect the actual parameter

```

Guess you like

Origin blog.51cto.com/13560219/2668426