func()里 可以访问全局变量i,但不能给i重新赋值。
i = 1
def func():
print( i + 1)
func() # 2
用global声明后,可以给i重新赋值。
i = 1
def func():
global i
i = i + 1
print(i)
func() # 2
2024-04-09 23:24:35
i = 1
def func():
print( i + 1)
func() # 2
i = 1
def func():
global i
i = i + 1
print(i)
func() # 2