嵌套函数中的局部变量

嵌套函数中的局部变量

好吧,请耐心等待我,我知道这看起来会非常令人费解,但请帮助我了解发生了什么。

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'打包'进入嵌套函数的局部范围?如果没有,对嵌套函数的调用如何查找局部变量?

我知道遇到这些问题通常意味着一个人“做错了”,但我想了解会发生什么。


摇曳的蔷薇
浏览 756回答 3
3回答

杨魅力

嵌套函数在执行时从父作用域查找变量,而不是在定义时查找。编译函数体,验证“自由”变量(未通过赋值在函数本身中定义),然后将闭包单元绑定到函数,代码使用索引引用每个单元格。pet_function因此具有一个自由变量(cage),然后将其通过一个闭合单元引用,索引为0的闭合本身指向局部变量cage在get_petters功能。当您实际调用该函数时,该闭包将用于查看函数调用时cage周围范围的值。这就是问题所在。当您调用函数时,该函数已经完成计算结果。将在在执行过程中的一些点局部变量分配各的,和字符串,但在功能的结束,包含了最后一个值。因此,当您调用每个动态返回的函数时,您将获得打印的值。get_petterscage'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在循环中定义函数,编译只发生一次,而不是在循环的每次迭代中发生。
打开App,查看更多内容
随时随地看视频慕课网APP