Where's the problem?
In Python, using global will make this function available. At the same time, accessing a variable inside a function is local and global .
In nested functions , using global creates an unnatural behavior.
On the code:
in []: def X (): b = def Y (): global b a = 1 b = 2 Y () print b = in [ 98]: del BIn [ 99]: X () b = 12in [ 100 100]: 1 in [ 101 101]: 2
in function x (), there is no use of global, and B at this time is local . So print prints the local b
Why is 12 printed? and in[101] B for 2 how to explain?
Y (), the use of the global did not put X () of the B = A guide in.
In function y (), the statement Global , A, and A/b extend to global, so at the highest level, B (in[101]) is produced even if there is no in[98.
That is , A and B are considered to be the outermost variables.
Try again:
In [102]:defx (): B= 12defy ():GlobalA, B= 1y ()Print "B =", B ....: In [103]: A = 111In [104]:delBIn [105]: X () b= 12In [106]: aout[106]: 1In [107]: b---------------------------------------------------------------------------Nameerror Traceback (most recent)<ipython-input-107-3b5d5c371295>inch<module>()----> 1Bnameerror:name'b' is notDefined
The error! Y () Global B is not assigned, and the top layer has no B. This means that global simply introduces the name and does not do the assignment.
Global does not have a variable that does not exist, only the name is imported, and the operation of that name responds to the ' highest level namespace '.
Again:
In [109]: a = 111In [110]:delb---------------------------------------------------------------------------Nameerror Traceback (most recent)<ipython-input-110-745f2abe7045>inch<module>()----> 1delBnameerror:name'b' is notDefinedin [111]:defx (): B= 12defy ():GlobalA, B= 1Printb y ()Print "B =", B ....: In [112]: X ()---------------------------------------------------------------------------Nameerror Traceback (most recent)<ipython-input-112-7354d77c61ac>inch<module>()----> 1x ()<ipython-input-111-c05fc67a1e82>inchx ()5 A = 1 6Printb----> 7y ()8Print "B =", b9 <ipython-input-111-c05fc67a1e82>inchy ()4GlobalA, b5 A = 1----> 6Printb7y ()8Print "B =", Bnameerror:GlobalName'b' is notDefined
This determines that the global of the Inner Y () does not import anything from X ().
So, how do the inner functions use the correct outer function variables?
Solving the problem of inner-layer function parameter transfer
One
First, if you just take a value, you don't need to do any processing.
def x (): .....: = ... : def y (): ...: Print a ...: y () ... : in [+]: X ()in[
In Y (), once A is assigned, a immediately changes the internal variable.
In [121]:defx (): .....: A= 12 .....: defy ():. ...:Print "before a =", a .....: A= 1 .....: Print "Then a =", a ....: Y () ....: in [122]: X () before a=---------------------------------------------------------------------------Unboundlocalerror Traceback (most recent)<ipython-input-122-7354d77c61ac>inch<module>()----> 1x ()<ipython-input-121-d8fbc0dba399>inchx ()5 A = 1 6Print "Then a =", a----> 7y ()8 <ipython-input-121-d8fbc0dba399>inchy ()2 A = 12 3defy ():----> 4Print "before a =", a5 A = 1 6Print "Then a =", aunboundlocalerror:local variable'a'Referenced before assignment
Once an assignment is given to a in the function y (), Python will assume that a does not exist before assigning a value.
Also found that the Python2 print will be one output. In view of this, I tried again in the Python3, found that he was output together. But this is not the focus of this article, folding it.
In [7]:defx (): a= 1defy ():Print("before A=", a) a= 10Print("Then a=", a) y () ...: in [8]: X ()---------------------------------------------------------------------------Unboundlocalerror Traceback (most recent)<ipython-input-8-7354d77c61ac>inch<module>()----> 1x ()<ipython-input-7-6e01e7317b24>inchx ()5 A = 10 6Print("Then a=", a)----> 7y ()8 <ipython-input-7-6e01e7317b24>inchy ()2 A = 1 3defy ():----> 4Print("before A=", a)5 A = 10 6Print("Then a=", a) Unboundlocalerror:local variable'a'Referenced before assignment
View Code
It is also found that Python code will be swept through the code before it runs, rather than just one line of execution.
Also found to return unboundlocalerror, not nameerror. Notice the ' unbound ', which is the official concept. The description of ' unbound ' is that global will bind the top-level variable name to the local variable name and change it to ' reference '; python detects a = 1 o'clock and realizes that a is local, so a ' point to an object ' (because the Python variable is a reference) before , calling a is illegal, but this behavior differs from Nameerror, which is defined as unbound local.
Two
Use mutable variables, such as List,dict
In [127]:defx (): .....: L= ["In msg"] .....: defy (): .....: Msg=l[0] ... ..:Print "msg =", Msg .....: l[:]= ["Out msg"] .....: Y () .....:Printl[0] ...: in [128]: X () msg=inchmsgout msg
No error, perfect!
Note that the statement l[:] = ["outmsg"] , using the slice assignment, otherwise,
In [129]:defx (): L= ["In msg"] defy (): Msg=L[0]Print "msg =", msg l= ["Out msg"] Y ()Printl[0] ...: in [130]: X ()---------------------------------------------------------------------------UnboundlocalerrorTraceback (most recent) <ipython-input-130-7354d77c61ac>In <Module> ()----> 1 x () <ipython-input-129-d44e750e285f> in X ()5Print "msg =", msg6 L = ["Out msg"]----> 7y ()8PrintL[0]9 <ipython-input-129-d44e750e285f> in Y ()2 L = ["In msg"] 3defy ():----> 4 msg =L[0]5Print "msg =", msg6 L = ["Out msg"]unboundlocalerror:local Variable'L'Referenced before assignment
Unboundlocalerror again, because the sixth line of code assigns a new list to L.
Third,
Use parameter passing.
def x (): ... := 1, 2 ....: def y (a = a, B = b): ... = 3, 4 .....:return A, b ....: = y () ..... : Print A, b ....: in [137]: X ()3 4
Note that mutable objects such as lists are not placed on default parameters.
Original, welcome to correct me.
The end of this article.
The use of global in Python, a puzzling question