嵌套函數中的局部變量好吧,請耐心等待我,我知道這看起來會非常令人費解,但請幫助我了解發生了什么。from functools import partialclass Cage(object):
def __init__(self, animal):
self.animal = animaldef gotimes(do_the_petting):
do_the_petting()def get_petters():
for animal in ['cow', 'dog', 'cat']:
cage = Cage(animal)
def pet_function():
print "Mary pets the " + cage.animal + "."
yield (animal, partial(gotimes, pet_function))funs = list(get_petters())for name, f in funs:
print name + ":",
f()得到:cow: Mary pets the cat.dog: Mary pets the cat.cat: Mary pets the cat.所以基本上,為什么我沒有得到三種不同的動物?是不是cage'打包'進入嵌套函數的局部范圍?如果沒有,對嵌套函數的調用如何查找局部變量?我知道遇到這些問題通常意味著一個人“做錯了”,但我想了解會發生什么。
3 回答

楊魅力
TA貢獻1811條經驗 獲得超6個贊
嵌套函數在執行時從父作用域查找變量,而不是在定義時查找。
編譯函數體,驗證“自由”變量(未通過賦值在函數本身中定義),然后將閉包單元綁定到函數,代碼使用索引引用每個單元格。pet_function
因此具有一個自由變量(cage
),然后將其通過一個閉合單元引用,索引為0的閉合本身指向局部變量cage
在get_petters
功能。
當您實際調用該函數時,該閉包將用于查看函數調用時cage
周圍范圍的值。這就是問題所在。當您調用函數時,該函數已經完成計算結果。將在在執行過程中的一些點局部變量分配各的,和字符串,但在功能的結束,包含了最后一個值。因此,當您調用每個動態返回的函數時,您將獲得打印的值。get_petters
cage
'cow'
'dog'
'cat'
cage
'cat'
'cat'
解決方法是不依賴于閉包。您可以使用部分函數,創建新的函數作用域,或將變量綁定為關鍵字參數的默認值。
部分功能示例,使用
functools.partial()
:from functools import partialdef pet_function(cage=None): print "Mary pets the " + cage.animal + "."yield (animal, partial(gotimes, partial(pet_function, cage=cage)))
創建新范圍示例:
def scoped_cage(cage=None): def pet_function(): print "Mary pets the " + cage.animal + "." return pet_functionyield (animal, partial(gotimes, scoped_cage(cage)))
將變量綁定為關鍵字參數的默認值:
def pet_function(cage=cage): print "Mary pets the " + cage.animal + "."yield (animal, partial(gotimes, pet_function))
不需要scoped_cage
在循環中定義函數,編譯只發生一次,而不是在循環的每次迭代中發生。
添加回答
舉報
0/150
提交
取消