小编典典

Python-嵌套函数中的局部变量

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“打包”到嵌套函数的局部作用域中?如果不是,对嵌套函数的调用如何查找局部变量?

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


阅读 924

收藏
2020-02-06

共1个答案

小编典典

嵌套函数在执行时(而不是在定义时)从父范围中查找变量。

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

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

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

部分函数示例,使用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在循环中定义函数,编译仅进行一次,而不是在循环的每次迭代中进行。

2020-02-06