程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

What do you not know about global and nonlocal in Python

編輯:Python

Python Global variables in 、 Local variables and global、nonlocal Keyword usage

1. Global and local variables

Preface :

  • Global variables are in the whole py Declaration in the document , Globally accessible ;
  • Local variables are declared in a function , It can only be called in this function , If you try to call... Where it is out of range , The program blew up
  • If you define a local variable with the same name as a global variable inside a function , It may lead to unexpected effects , Maybe not what you expected . Therefore, it is not recommended to use , This will make the program very unsound .

Yes 4 Rules , To distinguish whether a variable is in local scope or global scope :

  1. If the variable is used in the global scope ( That is, outside the function used ), It is always a global variable .
  2. If in a function , There are... For this variable global sentence , It's a global variable .
  3. otherwise , If the variable is used for an assignment statement in a function , It's a local variable .
  4. however , If the variable is not used in the assignment statement , It's a global variable .

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 .

  • In a function , A variable is either always local , Or always global variables . There is no way for the code in the function to use the name... First str1 Local variables of , Later we use global variables in the same function .
  • If you want to modify the value stored in the global variable in a function , You must use... For this variable global sentence .
  • In a function , If you try to use a local variable before it is assigned ,Python You're going to report a mistake . For example 2
# 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 .

2. global sentence

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 .

3. nonlocal sentence

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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved