Where's the problem?
In Python, global variables are used to make this function available. At the same time, accessing the variable inside the function will be local and global.
In nested functions, the use of global can produce perverse behavior.
Code on:
In [to]: Def x ():
b =
def y ():
global a,b
a = 1
b = 2
y ()
print "b =", B
....: In
[9 7]: a = in
[to]: del B in
[I]: x ()
b = [in
]: a
out[100]: 1 in
[[]: B
out[ 101]: 2
in function x (), no global is used, and b at this time uses local. So print prints local b
Why would I print 12? and In[101] 's B is 2 how to explain?
Y (), the use of global did not lead X () of B = 12 in.
In function y (), the statement global A,b the a,b to global, so at the highest level, even if there is no B (in[98]), B (in[101) is produced.
That is, the global a,b will think that A and B are the outermost variables.
Try again:
In [102]: Def x ():
b =
def y ():
global a,b
a = 1
y ()
print "b =", b
...: in
[a]: a = in [[]:
del B in
[]: x ()
B =
[a]
out[106]: 1 in
[[]: B
---------- -----------------------------------------------------------------
Nameerror Traceback (most recent call last)
<ipython-input-107-3b5d5c371295> in <module> ()
----> 1 b
nameerror:name ' B ' isn't Defined
It's an error! After Y () Global B is not assigned a value, there is no B at the top level. This means that global simply introduces names and does not perform assignments.
Global does not have the control that the variable exists, only the name is imported, and the operation on that name reacts to the ' top-level namespace '.
Again:
In [109]: a = "in
["]: del b
---------------------------------------------------------------------------
Nameerror Traceback (most recent call last)
<ipython-input-110-745f2abe7045> in <module> ()
- ---> 1 del b
nameerror:name ' B ' isn't defined in
[[]: Def x ():
b =
def y ():
Global a,b
a = 1
print b
y ()
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 = 1
6 print b
----> 7 y ()
8 print "b =", B
9
<ipython-input-111-c05fc67 A1e82> in Y ()
4 Global a,b
5 a = 1
----> 6 print B
7 y ()
8 print "b =", B
nameerror:gl Obal name ' B ' is not defined
This determines that the global of the Inner Y () does not import the X () thing.
So how does an inner function use the correct outer function variable?
Solving the problem of inner-layer function parameter passing
One
First, you don't need to do anything if you're just taking a value.
In [119]: Def x ():
...: a = ...
: def y ():....:
Print a
...: Y () ...: in [()
: X ()
12
In Y (), once a value is assigned to a, a immediately changes the internal variable.
In [121]: Def x ():
...: a = ....:
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 = 1
6 print "Then A =", a
----> 7 y ()
8
<ipython-input-121-d8fbc0dba399> In Y ()
2 A =
3 def y ():
----> 4 print "Before a =", a
5 a = 1
6 print "Then A =", a
UNB Oundlocalerror:local variable ' A ' referenced before assignment
Once you assign a value to a in the function y (), Python will assume that a does not exist before assigning a value.
At the same time found Python2 print will be one output. In view of this, I also tried in the Python3, found that he is output together. But this is not the focus of this article, folding it.
In [7]: Def x ():
a = 1
def y ():
print ("Before A=", a)
a = ten
print ("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 = ten
print ("Then A=", a)
---- > 7 y ()
<ipython-input-7-6e01e7317b24> in Y ()
a = 1
def y ():
----> 4 print ("Before A=", a
a = ten
print ("Then A=", a)
unboundlocalerror:local variable ' a ' referenced before
It also turns out that the Python code will sweep the code before it runs, not just a single line of execution.
Also found to return unboundlocalerror, not nameerror. Note the ' unbound ', which is an official concept. Use ' unbound ' to describe it: Global binds the top-level variable name to the local variable name, changing it to ' reference '; python detects a = 1 o'clock, realizes that a is local, so in a ' point to an object ' (because Python variables are all references), before , calling a is illegal, but this behavior differs from Nameerror, and is defined as unbound local.
Two
Use variable variables, such as List,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 = into msg out
msg
No error, perfect!
Note that the statement l[:] = ["Out msg"], using slices to assign values, otherwise,
In [129]: Def x ():
l = [' in msg ']
def y ():
msg = l[0]
print "msg =", msg
L = ["Out msg"]
y ()
PR int l[0] ...
: in
[130]: X ()
---------------------------------------------------------------------- -----
Unboundlocalerror Traceback (most recent) <ipython-input-130-7354d77c61ac> in
< Module> ()
----> 1 x ()
<ipython-input-129-d44e750e285f> in X ()
5 print "msg =", msg
6 L = ["Out msg"]
----> 7 y ()
8 print l[0]
9
<ipython-input-129-d44e750e285f> in Y ()
2 L = [' in msg ']
3 D EF y ():
----> 4 msg = l[0]
5 print "msg =", msg
6 L = ["Out msg"]
unboundlocalerror:local variable ' L ' referenced before assignment
Another unboundlocalerror, because the sixth line of code assigns a new list to L.
Three
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 that you do not place variable objects such as list on the default parameters.
The above is a small set to introduce Python in the use of global caused by a series of problems, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!