[转]python变量作用域的有趣差别
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
i = 1
def func():
print( i + 1)
func() # 2
i = 1
def func():
global i
i = i + 1
print(i)
func() # 2