[转]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
posted @ 2017-09-11 13:33  bingo彬哥  阅读(196)  评论(0编辑  收藏  举报
本站总访问量