Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 475 Bytes

156.md

File metadata and controls

34 lines (24 loc) · 475 Bytes
@author jackzhenguo
@desc
@tag
@version 
@date 2020/02/23

共享变量未绑定之坑

有时想要多个函数共享一个全局变量,但却在某个函数内试图修改它为局部变量:

i = 1
def f():
    i+=1 #NO!
    
def g():
    print(i)

应该在f函数内显示声明i为global变量:

i = 1
def f():
    global i # YES!
    i+=1
[上一个例子](155.md) [下一个例子](157.md)