Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套函数中的局部变量_Python_Scope_Closures_Nested Function - Fatal编程技术网

Python 嵌套函数中的局部变量

Python 嵌套函数中的局部变量,python,scope,closures,nested-function,Python,Scope,Closures,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'

好吧,请耐心听我说,我知道这看起来会非常复杂,但请帮助我理解发生了什么

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_函数因此有一个自由变量(
cage
),然后通过索引0的闭合单元格引用该变量。闭包本身指向
get_peters
函数中的局部变量
cage

当您实际调用函数时,该闭包随后用于在调用函数时查看周围范围中的
cage
值。问题就在这里。当您调用函数时,
get\u peters
函数已经完成了对结果的计算。在执行过程中的某个时间点,将
cage
局部变量分配给
'cow'
'dog'
'cat'
字符串,但在函数末尾,
cage
包含最后一个值
'cat'
。因此,当您调用每个动态返回的函数时,都会打印值
'cat'

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

  • 部分函数示例,使用:

  • 创建新范围示例:

    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))
    

无需在循环中定义作用域函数,编译只发生一次,而不是循环的每次迭代。

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

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

当您实际调用函数时,该闭包随后用于在调用函数时查看周围范围中的
cage
值。问题就在这里。当您调用函数时,
get\u peters
函数已经完成了对结果的计算。在执行过程中的某个时间点,将
cage
局部变量分配给
'cow'
'dog'
'cat'
字符串,但在函数末尾,
cage
包含最后一个值
'cat'
。因此,当您调用每个动态返回的函数时,都会打印值
'cat'

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

  • 部分函数示例,使用:

  • 创建新范围示例:

    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))
    

无需在循环中定义
作用域的
函数,编译只发生一次,而不是在循环的每次迭代中进行。

这源于以下内容

for i in range(2): 
    pass

print(i)  # prints 1
迭代后,
i
的值被延迟存储为其最终值


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

这源于以下原因

for i in range(2): 
    pass

print(i)  # prints 1
迭代后,
i
的值被延迟存储为其最终值


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

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

所以当你这么做的时候

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.

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

所以当你这么做的时候

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.

让我们简化这个问题。定义:

def get_petters():
    for animal in ['cow', 'dog', 'cat']:
        def pet_function():
            return "Mary pets the " + animal + "."

        yield (animal, pet_function)
然后,就像问题中一样,我们得到:

>>> for name, f in list(get_petters()):
...     print(name + ":", f())

cow: Mary pets the cat.
dog: Mary pets the cat.
cat: Mary pets the cat.
但是如果我们避免先创建
列表()

>>> for name, f in get_petters():
...     print(name + ":", f())

cow: Mary pets the cow.
dog: Mary pets the dog.
cat: Mary pets the cat.
发生什么事了?为什么这种细微的差异会完全改变我们的结果


如果我们看一下
列表(get_peters())
,从不断变化的内存地址可以清楚地看出,我们确实产生了三种不同的函数:

>>> list(get_petters())

[('cow', <function get_petters.<locals>.pet_function at 0x7ff2b988d790>),
 ('dog', <function get_petters.<locals>.pet_function at 0x7ff2c18f51f0>),
 ('cat', <function get_petters.<locals>.pet_function at 0x7ff2c14a9f70>)]
对于这两个循环,对象在整个迭代过程中保持不变。然而,正如预期的那样,它所引用的特定的
str