This error occurs, usually because, in the function call, using the result of external variables. Here to harm no matter what your goal is (may be you want some of this external variable assignment; it may be that you just want to create a local variable, but you do not accidentally inflicted and external variables of the same name)
The reason given
0. First, assuming you do not have to call in the beginning of the use of the internal functions global External _variable#外部变量
, or nonlocal External _variable#外部变量
(because of the use of these two statements, the program will only be considered External _variable
as an external variable, it is impossible to be incorrect report)
1. The external call to an internal function, variable assignment, then the time you actually think the program is carried out in the assignment of a variable with the same name outside the local variables.
2. Since it is a local variable assignment, then the other operations (these operations require this local variable has been assigned the initial value) must appear after the assignment statement.
3. However, if other actions appear before the assignment, then the program error.
4. There is a special case External _variable += 2
, because it is the assignment operator (so the program determines that this variable internal variable), also belong to other operations, as it requires over this initial value the variable a. This situation will be given.
The function assignment is not performed, when the reference, there is no problem using a direct external variables, such as for print
the operation.
basic type
When this category is external variables basic types (such as int, float, str, etc.).
Operation of the internal function of the following situations:
Only initial value, no other operations
global External _variable#外部变量
At this point the program is very harmonious, as local variables within the function, external function as an external variable.
There are initial value, there are other operations
And other operations after the initial value does not appear in the operation.
nonlocal External _variable#外部变量
The program should consider two
local variables, but two
even the initial values are not, let others carry out +=
the operation, it will certainly be an error.
Non-basic categories
When the type of the non-basic types of external variables (such as list, dict, etc.).
Operation of the internal function of the following situations:
No initial value operation, there are other operations
External _variable
Print results can be seen, the program considered two
as external variables, executing the functions, external variables has changed.
Only initial value, whether or not other operations
External _variable += 2
No other operations: Because of assignment within a function, then the program considered two
as a local variable.
print
Other operations, and the order is right (i.e., previous assignment): because of the assignment function, the program at this time that two
the local variables. No matter how much you have other operations, operations to just the local variables only.
Other operations assignments before operation
two = 0
def add_two():
two = 2
print(two)
add_two()
print(two)
Other operations, and the order is wrong (i.e. later assignment): because of the assignment function, the program at this time that two
the local variables. But the order was wrong, so the error.