想法
在Python的哲學里,函數不強制要有返回值,
對於沒有reutrn的函數解釋器會自作主張返回一個None
因此,可以用函數實現過程封裝。
問題
函數內部變量都是局部的,相當於namespace限定在這個函數里,無法影響全局,例如:
>>> def init():
... x=0
...
>>> init()
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
解決
使用global關鍵字聲明變量為全局有效
>>> def super_init():
... global x
... x = 0
...
>>> super_init()
>>> x
0