Python嵌套函数中的局部变量
好吧,对我承担这一点,我知道这将看起来非常令人费解,但请帮助我了解发生了什么。
from functools import partial
class Cage(object):
    def __init__(self, animal):
        self.animal = animal
def 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不是被“包装”到嵌套功能的局部范围内吗?  如果不是,调用嵌套函数如何查找局部变量? 
我知道遇到这种问题通常意味着“做错了”,但我想知道发生了什么。
嵌套函数在执行时从父范围查找变量,而不是在定义时查找。
  函数体被编译,并且'自由'变量(未在函数本身中通过赋值定义)被验证,然后作为闭包单元绑定到函数,代码使用索引来引用每个单元格。  因此, pet_function有一个自由变量( cage ),然后通过闭合单元索引0引用它。闭合本身指向get_petters函数中的局部变量cage 。 
  实际调用该函数时,在调用该函数时,该闭包将用于查看周围范围内的cage值。  这就是问题所在。  当你调用你的函数时, get_petters函数已经完成了计算结果。  在执行期间的某个点处的cage局部变量被分配了'cow' , 'dog'和'cat'串的每一个,但是在该函数结束时, cage包含最后一个值'cat' 。  因此,当你调用每个动态返回的函数时,你会得到打印的值'cat' 。 
解决方法是不依赖闭包。 您可以改为使用部分函数,创建新的函数作用域,或将该变量绑定为关键字参数的默认值。
  部分函数示例,使用functools.partial() : 
from functools import partial
def 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_function
yield (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函数,编译只发生一次,而不是循环的每次迭代。 
我的理解是,当实际调用了yield的pet_function时,在父函数名称空间中寻找笼子,而不是之前。
所以,当你这样做
funs = list(get_petters())
您会生成3个函数,它们将查找最后创建的笼子。
如果你用最后一个循环替换:
for name, f in get_petters():
    print name + ":", 
    f()
你会得到:
cow: Mary pets the cow.
dog: Mary pets the dog.
cat: Mary pets the cat.
这源于以下
for i in range(2): 
    pass
print i is 1
  迭代后, i的值被懒惰地存储为它的最终值。 
  作为一个生成器,函数可以工作(即依次打印每个值),但是当转换为列表时,它将运行在生成器上 ,因此所有对cage ( cage.animal )的调用cage.animal返回cage.animal 。 
