a = 1
def func():
global a
a += 1
func()
print(a)
#输出2
a = 1
def func():
print(a)
global a
a += 1
func()
print(a)
#报错:name ‘a’,is used prior to global declaration
#正确写法
a = 1
def func():
global a
print(a)
a += 1
func()
print(a)
#不止在修改前,在调用前也要声明global
python global
最新推荐文章于 2024-09-07 20:08:36 发布