嵌套函数中的局部变量

noi*_*oio 102 python closures scope nested-function

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

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()
Run Code Online (Sandbox Code Playgroud)

得到:

cow: Mary pets the cat.
dog: Mary pets the cat.
cat: Mary pets the cat.
Run Code Online (Sandbox Code Playgroud)

所以基本上,为什么我没有得到三种不同的动物?是不是cage'打包'进入嵌套函数的局部范围?如果没有,对嵌套函数的调用如何查找局部变量?

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

Mar*_*ers 110

嵌套函数在执行时从父作用域查找变量,而不是在定义时查找.

编译函数体,验证"自由"变量(未通过赋值在函数本身中定义),然后将闭包单元绑定到函数,代码使用索引引用每个单元格.pet_function因此具有一个自由变量(cage),然后将其通过一个闭合单元引用,索引为0的闭合本身指向局部变量cageget_petters功能.

当您实际调用该函数时,该闭包将用于查看函数调用时cage周围范围的值.这就是问题所在.当您调用函数时,该函数已经完成计算结果.将在在执行过程中的一些点局部变量分配各的,和字符串,但在功能的结束,包含了最后一个值.因此,当您调用每个动态返回的函数时,您将获得打印的值.get_petterscage'cow''dog''cat'cage'cat''cat'

解决方法是不依赖于闭包.您可以使用部分函数,创建新的函数作用域,或将变量绑定为关键字参数默认值.

不需要scoped_cage在循环中定义函数,编译只发生一次,而不是在循环的每次迭代中发生.


Nic*_*bey 12

我的理解是,当实际调用生成的pet_function时,在父函数命名空间中查找cage,而不是之前.

所以,当你这样做

funs = list(get_petters())
Run Code Online (Sandbox Code Playgroud)

您生成3个函数,可以找到最后创建的笼子.

如果用以下代码替换上一个循环:

for name, f in get_petters():
    print name + ":", 
    f()
Run Code Online (Sandbox Code Playgroud)

你会真正得到:

cow: Mary pets the cow.
dog: Mary pets the dog.
cat: Mary pets the cat.
Run Code Online (Sandbox Code Playgroud)


And*_*den 6

这源于以下内容

for i in range(2): 
    pass

print i is 1
Run Code Online (Sandbox Code Playgroud)

在迭代之后,i将lazily 的值存储为其最终值.

作为一个生成器,该函数可以工作(即依次打印每个值),但是当转换为列表时,它会在生成器上运行,因此对cage(cage.animal)的所有调用都会返回cat.