Static local name Detection
When Python detects local scopes: It detects code compilation in python, rather than assigning values at runtime.
Under normal circumstances, the name that is not copied in the function will be searched in the module containing it:
>>> X = 99
>>> Def selector ():
... Print x
...
>>> Selector ()
99
However:
>>> Def selector ():
... Print x
... X = 100
...
>>> Selector ()
Traceback (most recent call last ):
File "<stdin>", line 1, in?
File "<stdin>", line 2, in selector
UnboundLocalError: local variable 'X' referenced before assignment
Will get an error of undefined name.
When interactive input or imported from a module, python reads and compiles this code. When compiling, python checks the value assignment of x, it is determined that x will be a local name anywhere in the function. After that, the function is actually running. During print execution, the value assignment has not yet occurred. python will say that you are using an undefined name. According to his name rule, the local x should be used before the value assignment.
Solution:
If you want to print global x, you should declare in the global statement: (this means that the assignment also changes global x, not local x)
>>> Def selector ():
... Global x
... Print x
... X = 88
...
>>> Selector ()
99
If you want to print a Global Value assignment, set a local value, import the modules that contain the global value, and use the following command to get the global version:
>>> X = 99
>>> Def selector ():
... Import _ main __
... Print _ main _. x
... X = 88
... Print x
...
>>> Selector ()
99
88
Limit (. x) returns a value from a namespace object. The namespace of the interaction environment is a module called _ main.
Nested functions can be nested with scopes (different in the new and old versions)
>>> Def outer (x ):
... Def inner (I ):
... Print I,
... If I: inner (I-1)
... Inner (x)
...
>>> Outer (3)
3 2 1 0
Save reference by default
>>> Def outer (x ):
... Def inner (I, self = inner ):
... Print I,
... If I: self (I-1)
... Inner (x)
...
>>> Outer (3)
Traceback (most recent call last ):
File "<stdin>", line 1, in?
File "<stdin>", line 2, in outer
UnboundLocalError: local variable 'inner 'referenced before assignment
Solution Principle: The simplest way is always the most correct
>>> Def inner (I ):
... Print I,
... If I: inner (I-1)
...
>>> Def outer (x ):
... Inner (x)
...
>>> Outer (3)
3 2 1 0
Default variable object
>>> Def saver (x = []):
... X. append (1)
... Print x
...
>>> Saver ([2])
[2, 1]
>>> Saver ()
[1]
>>> Saver ()
[1, 1]
>>> Saver ()
[1, 1, 1]
The problem is that there is only one list object generated when def is executed. When each callback function is called, you will not get a new list object, but the growth of the original list object.
Solution: if it is not the behavior you want, simply move the default value to the function body. As long as the value in the code is executed when every worker function is running, you will get a new object each time:
>>> Def saver (x = None ):
... If x is None:
... X = []
... X. append (1)
... Print x
...
>>> Saver ([2])
[2, 1]
>>> Saver ()
[1]
>>> Saver ()
[1]
>>> Saver ()
[1]
The above if statement can be almost replaced by the value x = x or [], because or of python will return one of its operation objects: if no parameter is passed, x is set to None by default, therefore, or returns an empty list on the right. But this is not exactly the same. When an empty list is passed, the function will expand and return a new list, instead of extending and returning a passed list as in the previous version (the expression is changed to [] or [], which will calculate the new list)