(1)global
:修改全局变量
a=1
def f():
a=2
def g():
global a
a=3
print(a) #3
g()
print(a) #2
f()
print(a) #3
(2)nonlocal
:修改外层词法变量
a=1
def f():
a=2
def g():
nonlocal a
a=3
print(a) #3
g()
print(a) #3
f()
print(a) #1
(3)default:创建局部变量
a=1
def f():
a=2
def g():
a=3
print(a) #3
g()
print(a) #2
f()
print(a) #1
网友评论