原创:https://www.cnblogs.com/z360519549/p/5172020.html
python引用变量的顺序: 当前作用域局部变量-> 闭包函数外的函数中变量->当前模块中的全局变量->python内置变量 。
一 、global
global关键字用来在函数或其他局部作用域中使用全局变量。但是如果不修改全局变量也可以不使用global关键字。
gcount = 0
def global_test():
gcount+=1
print (gcount)
global_test()
D:\Python34\python.exe E:/PycharmProjects/Day3/globaltest.py
Traceback (most recent call last):
File “E:/PycharmProjects/Day3/globaltest.py”, line 6, in
global_test()
File “E:/PycharmProjects/Day3/globaltest.py”, line 4, in global_test
gcount+=1
UnboundLocalError: local variable ‘gcount’ referenced before assignment
Process finished with exit code 1
第一行定义了一个全局变量,(可以省略global关键字)。
在global_test 函数中程序会因为“如果内部函数有引用外部函数的同名变量或者全局变量,并且对这个变量有修改.那么python会认为它是一个局部变量,又因为函数中没有gcount的定义和赋值,所以报错。
二、声明全局变量,如果在局部要对全局变量修改,需要在局部也要先声明该全局变量:
gcount = 0
def global_test():
global gcount
gcount+=1
print (gcount)
global_test()
如果在函数中声明 gcount 是全局变量,即可对其进行修改。 正确输出 1 。
三、 在局部如果不声明全局变量,并且不修改全局变量。则可以正常使用全局变量:
gcount = 0
def global_test():
print (gcount)
global_test()
如果在局部不修改全局变量,程序正确输出 0 。
四、nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。
def make_counter():
count = 0
def counter():
nonlocal count
count += 1
return count
return counter
def make_counter_test():
mc = make_counter()
print(mc())
print(mc())
print(mc())
make_counter_test()
'''
输出:
1
2
3
'''
五、实例
1、
def scope_test():
def do_local():
spam = "local spam" #此函数定义了另外的一个spam字符串变量,并且生命周期只在此函数内。此处的spam和外层的spam是两个变量,如果写出spam = spam + “local spam” 会报错
def do_nonlocal():
nonlocal spam #使用外层的spam变量
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignmane:", spam)
do_nonlocal()
print("After nonlocal assignment:",spam)
do_global()
print("After global assignment:",spam)
scope_test()
print("In global scope:",spam)
'''
输出是:
After local assignmane: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
'''
2、
def add_b():
global b
b = 42
def do_global():
global b
b = b + 10
print(b)
do_global()
print(b)
add_b()
print(b)
'''
以上代码输出:
52
52
52
'''
def add_b():
global b
b = 42
def do_global():
#global b
b = b + 10
print(b)
do_global()
print(b)
add_b()
'''
以上代码报错:
Traceback (most recent call last):
File "E:/PycharmProjects/OOP/exe1.py", line 42, in
add_b()
File "E:/PycharmProjects/OOP/exe1.py", line 40, in add_b
do_global()
File "E:/PycharmProjects/OOP/exe1.py", line 38, in do_global
b = b + 10
UnboundLocalError: local variable 'b' referenced before assignment
原因: global 定义的 b ,只能引用,不能修改。
'''
def add_b():
global b
b = 42
def do_global():
global a
a = b + 10
print(b)
do_global()
print(a)
add_b()
print("a = %s , b = %s " %(a, b))
'''
输出:
42
52
a = 52 , b = 42
'''
def add_b():
#global b
b = 42
def do_global():
global b
b = 10
print(b)
do_global()
print(b)
add_b()
print(" b = %s " % b)
'''
以上代码输出:
10
42
b = 10
'''
def add_b():
#global b
b = 42
def do_global():
nonlocal b
b = 10
print(b)
do_global()
print(b)
add_b()
'''
以上代码输出:
10
10
'''
def add_b():
#global b
b = 42
def do_global():
nonlocal b
b = 10
print(b)
do_global()
print(b)
add_b()
print(" b = %s " % b)
'''
以上代码报错:
print(" b = %s " % b)
NameError: name 'b' is not defined
说明: nonlocal 适用于在局部函数 中 的局部函数, 把最内层的局部 变量设置成外层局部可用,但是还不是全局的。
'''
def add_b():
#global b
#b = 42
def do_global():
nonlocal b
b = 10
print(b)
do_global()
#print(b)
add_b()
'''
以上代码报错:
File "E:/PycharmProjects/OOP/exe1.py", line 37
nonlocal b
SyntaxError: no binding for nonlocal 'b' found
nonlocal 要绑定一个局部变量。
'''
def add_b():
#global b
#b = 42
def do_global():
global b
b = 10
print(b)
do_global()
print(b)
add_b()
print(" b = %s " % b)
'''
以上代码输出:
10
10
b = 10
'''
def add_b():
#global b
#b = 42
def do_global():
global b
b = 10
print(b)
do_global()
#b = b + 20
print(b)
add_b()
b = b + 30
print(" b = %s " % b)
'''
以上代码输出:
10
10
b = 40
'''
def add_b():
#global b
#b = 42
def do_global():
global b
b = 10
print(b)
do_global()
b = b + 20
print(b)
add_b()
b = b + 30
print(" b = %s " % b)
'''
以上代码报错:
b = b + 20
UnboundLocalError: local variable 'b' referenced before assignment
修改如下:
'''
def add_b():
#global b
b = 42
def do_global():
global b
b = 10
print(b)
do_global()
b = b + 5
print(b)
add_b()
b = b + 30
print(" b = %s " % b)
'''
以上代码输出:
10
47
b = 40
'''