Preface :
Yes 4 Rules , To distinguish whether a variable is in local scope or global scope :
read example 1, These rules can be better understood ,
# example 1
def test1():
global str1
str1 = 'hello' # This is a global variable
def test2():
str1 = 'python' # This is a local variable
def test3():
print(str1) # This is a global variable
str1 = 42 # This is a global variable
test1()
print(str1) # out: hello
explain : stay test1() Function ,str1 It's the whole picture str1 Variable , Because at the beginning of the function , Targeted str1 Variable global sentence . stay test2() Function ,str1 It's a local variable , Because there is an assignment statement for it in this function . stay test3() Function ,str1 Global variable , Because in this function , There are no assignment statements , And there's nothing against it global sentence .
# example 2
def test1():
print(str1) # ERROR
str1 = 'hello world' # local variable
str1 = 'hi' # Global variables
test1()
''' Report errors : File "E:/test/test.py", line 92, in test1 print(str1) # ERROR UnboundLocalError: local variable 'str1' referenced before assignment '''
example 2 explain : The reason for the error , because Python notice test1() The function is for str1 Assignment statement for , So I think str1 Variables are local variables . however print(str1) The statement is executed in str1 Before assignment , local variable str1 There is no such thing as ( That is, local variables are applied before assignment str1).Python Will not be returned to use global str1 Variable .
Use global Statement can modify global variables within a function . If you have at the top of the function " global str1 " This code , It tells me Python ,“ In this function ,str1 Refers to global variables , So don't create a local variable with this name ”. For example, the following code :
def test1():
global str1
str1 = 'one'
str1 = 'global'
test1()
print(str1) # out: one
because str1 stay test1() The top of is declared gloabl, So when str1 To be an assignment ‘one’ when , The assignment takes place in the global scope str1 On . No local str1 Variable .
Python 3.0 A new nonlocal sentence , actually , When executed nonlocal At the time of statement ,nonlocal The names listed in must be in a nested def Defined in advance , otherwise , There will be a mistake .nonlocal Names can only appear in nested def in , It cannot be in the global scope of a module or def In the built-in scope beyond .
Represents in a local scope , Call variables in the parent scope ; If there are many nested layers , At most, it can only act on the top-level function , Cannot act on global variables .
Work out these problems , Theory should be combined with practice
a = 1
def func_1():
a =2
def func_2():
# a = 3 # modular A
def func_3():
nonlocal a
a = 4
print(' 8 That's ok :',a)
print(' 9 That's ok :',a)
func_3()
print('11 That's ok :',a)#2 --> 4
print('12 That's ok :',a)
func_2()
print('14 That's ok :',a)#2-->4
print('15 That's ok :',a)
func_1()
print('17 That's ok :',a)# Global variables ,nonlocal Have no right to change
In the above code , Add modules A Result :
No modules A Result :