Python函數學習筆記
2020-02-23 04:39:58
供稿:網友
局部名字靜態檢測
Python探測局部作用域的時候:是在python編譯代碼時檢測,而不是通過他們在運行時的賦值。
正常的情況下,沒在函數中復制的名字將在包含它的模塊中查找:
>>> x=99
>>> def selector():
... print x
...
>>> selector()
99
但是:
>>> def selector():
... print x
... x=100
...
>>> selector()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in selector
UnboundLocalError: local variable 'x' referenced before assignment
會得到未定義名字的錯誤。
當 交互式輸入或從一個模塊中導入時,python讀取并編譯這段代碼,當編譯時python查看x的賦值,并決定在函數中任何地方x都將是個局部名字。到后 來函數真的運行,print執行時,賦值還沒有發生,python會說你正在使用一個未定義的名字。根據他的名字規則,應該是局部的x在賦值前被使用了。
解決辦法:
如果你想打印全局x,你應該在global語句中聲明:(這意味著該賦值也改變全局x,而不是局部x)
>>> def selector():
... global x
... print x
... x=88
...
>>> selector()
99
如果你想打印出全局賦值,在設定一個局部的,導入包含它的模塊并用限定得到這個全局的版本:
>>> x=99
>>> def selector():
... import __main__
... print __main__.x
... x=88
... print x
...
>>> selector()
99
88
限定(.x部分)從一個名字空間對象中得到一個值。交互環境的名字空間是一個叫做__main__的模塊。
嵌套函數可以嵌套作用域(在新版本中和老版本中不同)
>>> def outer(x):
... def inner(i):
... print i,
... if i: inner(i-1)
... inner(x)
...
>>> outer(3)
3 2 1 0
使用默認值保存引用
>>> def outer(x):
... def inner(i,self=inner):
... print i,
... if i:self(i-1)
... inner(x)
...
>>> outer(3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in outer
UnboundLocalError: local variable 'inner' referenced before assignment
解決原則:最簡單的方式總是最正確的方式
>>> def inner(i):
... print i,
... if i:inner(i-1)
...
>>> def outer(x):
... inner(x)
...
>>> outer(3)
3 2 1 0
默認的可變對象
>>> def saver(x=[]):
... x.append(1)
... print x
...
>>> saver([2])
[2, 1]
>>> saver()
[1]
>>> saver()
[1, 1]
>>> saver()
[1, 1, 1]
問題是,這里只有一個列表對象——def執行時生成的一個。在每一次函數被調用時,你不會得到新的列表對象,而是原列表對象的增長。