A series of problems caused by using GLOBAL in PYTHON, pythonglobal
What's wrong?
In python, global is used to set global variables to be available for this function. At the same time, accessing the variable inside the function will first be local and then global.
Using global in nested functions produces unreasonable behavior.
Code:
In [96]: def x():b = 12def y():global a,ba = 1b = 2y()print "b =",b....: In [97]: a = 111In [98]: del bIn [99]: x()b = 12In [100]: aOut[100]: 1In [101]: bOut[101]: 2
In Function x (), global is not used. In this case, B uses local. So print will print local B
Why does it print 12? What should I do if B of In [101] is 2?
Y (), the global used did not export B = 12 of x.
In the y () function, the statements global a and B Make a and B expand to the global, so at the highest level, even if there is no B (In [98]), B (In [101]) is also generated.
That is to say, global a and B think that a and B are the outermost variables.
Try again:
In [102]: def x():b = 12def y():global a,ba = 1y() print "b =",b.....: In [103]: a = 111In [104]: del bIn [105]: x()b = 12In [106]: aOut[106]: 1In [107]: b---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-107-3b5d5c371295> in <module>()----> 1 bNameError: name 'b' is not defined
An error is reported! If no value is assigned after y () global B, no value is assigned to B on the top layer. This indicates that global only introduces the name and does not assign values.
Global does not care about the existence or absence of variables, but only imports the name. The operation on the name will reflect the 'highest namespace '.
Again:
In [109]: a = 111In [110]: del b---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-110-745f2abe7045> in <module>()----> 1 del bNameError: name 'b' is not definedIn [111]: def x():b = 12def y():global a,ba = 1print by()print "b =",b.....: In [112]: x()---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-112-7354d77c61ac> in <module>()----> 1 x()<ipython-input-111-c05fc67a1e82> in x()5 a = 16 print b----> 7 y()8 print "b =",b9 <ipython-input-111-c05fc67a1e82> in y()4 global a,b5 a = 1----> 6 print b7 y()8 print "b =",bNameError: global name 'b' is not defined
This determines that the global of the inner y () won't import x.
Then, how can an inner function use the correct outer function variable?
Solve the Problem of passing internal function parameters
I,
First, if it is only a value, no processing is required.
In [119]: def x():.....: a = 12.....: def y():.....: print a.....: y().....: In [120]: x()12In [121]:
In y (), once a is assigned a value, a immediately changes the internal variable.
In [121]: def x():.....: a = 12.....: def y():.....: print "before a =",a.....: a = 1.....: print "then a =",a.....: y().....: In [122]: x()before a =---------------------------------------------------------------------------UnboundLocalError Traceback (most recent call last)<ipython-input-122-7354d77c61ac> in <module>()----> 1 x()<ipython-input-121-d8fbc0dba399> in x()5 a = 16 print "then a =",a----> 7 y()8 <ipython-input-121-d8fbc0dba399> in y()2 a = 123 def y():----> 4 print "before a =",a5 a = 16 print "then a =",aUnboundLocalError: local variable 'a' referenced before assignment
Once a value is assigned to a somewhere in Function y (), python determines that a does not exist before the value is assigned.
At the same time, it is found that the print of python2 will be output one by one. In view of this, I tried again in python3 and found that it was output together. But this is not the focus of this Article. Fold it.
In [7]: def x():a = 1def y():print("before a=",a)a = 10print("then a=",a)y()...: In [8]: x()---------------------------------------------------------------------------UnboundLocalError Traceback (most recent call last)<ipython-input-8-7354d77c61ac> in <module>()----> 1 x()<ipython-input-7-6e01e7317b24> in x()a = 10print("then a=",a)----> 7 y()<ipython-input-7-6e01e7317b24> in y()a = 1def y():----> 4 print("before a=",a)a = 10print("then a=",a)UnboundLocalError: local variable 'a' referenced before assignment
At the same time, it is found that the python code will be scanned first before it is run, rather than simply executing one line at a time.
At the same time, UnboundLocalError instead of NameError is returned. Note that 'unbound' is an official concept. 'Unbound' is used to describe that global will bind the top-level variable name to the local variable name and change it to 'quot'. When python detects a = 1, realize that a is local, so before a' points to an object '(because python variables are referenced), calling a is illegal, but this behavior is different from NameError, it is defined as unbound local.
II,
Use variable variables, such as list and dict
In [127]: def x():.....: l = ["in msg"].....: def y():.....: msg = l[0].....: print "msg =",msg.....: l[:] = ["out msg"].....: y().....: print l[0].....: In [128]: x()msg = in msgout msg
No error reported. Perfect!
Note that the statement l [:] = ["out msg"] is used to assign values to slices. Otherwise,
In [129]: def x():l = ["in msg"]def y():msg = l[0]print "msg =",msgl = ["out msg"]y()print l[0].....: In [130]: x()---------------------------------------------------------------------------UnboundLocalError Traceback (most recent call last)<ipython-input-130-7354d77c61ac> in <module>()----> 1 x()<ipython-input-129-d44e750e285f> in x()5 print "msg =",msg6 l = ["out msg"]----> 7 y()8 print l[0]9 <ipython-input-129-d44e750e285f> in y()2 l = ["in msg"]3 def y():----> 4 msg = l[0]5 print "msg =",msg6 l = ["out msg"]UnboundLocalError: local variable 'l' referenced before assignment
UnboundLocalError occurs again, because the sixth line of code assigns a new list to l.
III,
Use parameters to pass.
In [136]: def x():.....: a, b = 1, 2.....: def y(a = a, b = b):.....: a, b = 3, 4.....: return a, b.....: a, b = y().....: print a, b.....: In [137]: x()3 4
Note: Do not place variable objects such as list on default parameters.
The above is a series of problems caused by the use of GLOBAL in PYTHON. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!