2016年5月11日 星期三

python 使用 global 時機

當 local 區域 要 assign 資料給 外部區域 的變數時,
就必須先在 local 區域 使用 global  宣告一個相同名稱的 區域變數:

g = 1

def  func():
global g
g = 2
若是單純取值,沒有 assign 的動作,則可以不宣告:
g = 1
def func():
print(g)

 

沒有留言: