【问题标题】:Argument binding of python 3 lambda appears to be brokenpython 3 lambda 的参数绑定似乎被破坏了
【发布时间】:2020-12-24 19:26:10
【问题描述】:

我在 CentOS 7 环境中使用 Python 3.6.10。我正在尝试根据结构化规范创建要执行的命令列表。将其视为 lambda 列表似乎很自然和 Pythonic。我通过遍历规范来构建 lambda 列表。令我惊讶的是,当我执行结果时,我发现每个 lambda 都是相同的,因为它在创建 lambda 时没有捕获其参数。我认为这是一个错误。

以下是说明该行为的示例代码:

specification = {
    'labelOne': ['labelOne.one', 'labelOne.two', 'labelOne.three', 'labelOne.four', 'labelOne.five'],
    'labelTwo': ['labelTwo.one', 'labelTwo.two', 'labelTwo.three', 'labelTwo.four', 'labelTwo.five'],
    'labelThree': ['labelThree.one', 'labelThree.two', 'labelThree.three', 'labelThree.four', 'labelThree.five'],
    'labelFour': ['labelFour.one', 'labelFour.two', 'labelFour.three', 'labelFour.four', 'labelFour.five'],
    'labelFive': ['labelFive.one', 'labelFive.two', 'labelFive.three', 'labelFive.four', 'labelFive.five'],
    }

lambdas = []
for label, labelStrings in specification.items():
    for labelString in labelStrings:
        lambdaString = f"""Label: \"{label}\" with labelString: \"{labelString}\""""
        oneArgLambda = lambda someArg: print(someArg, lambdaString)
        lambdas.append(oneArgLambda)

for each in lambdas:
    each('Show: ')

我希望看到这个:

Show:  Label: "labelOne" with labelString: "labelOne.one"
Show:  Label: "labelOne" with labelString: "labelOne.two"
Show:  Label: "labelOne" with labelString: "labelOne.three"
Show:  Label: "labelOne" with labelString: "labelOne.four"
Show:  Label: "labelOne" with labelString: "labelOne.five"
Show:  Label: "labelTwo" with labelString: "labelTwo.one"
Show:  Label: "labelTwo" with labelString: "labelTwo.two"
Show:  Label: "labelTwo" with labelString: "labelTwo.three"
Show:  Label: "labelTwo" with labelString: "labelTwo.four"
Show:  Label: "labelTwo" with labelString: "labelTwo.five"
Show:  Label: "labelThree" with labelString: "labelThree.one"
Show:  Label: "labelThree" with labelString: "labelThree.two"
Show:  Label: "labelThree" with labelString: "labelThree.three"
Show:  Label: "labelThree" with labelString: "labelThree.four"
Show:  Label: "labelThree" with labelString: "labelThree.five"
Show:  Label: "labelFour" with labelString: "labelFour.one"
Show:  Label: "labelFour" with labelString: "labelFour.two"
Show:  Label: "labelFour" with labelString: "labelFour.three"
Show:  Label: "labelFour" with labelString: "labelFour.four"
Show:  Label: "labelFour" with labelString: "labelFour.five"
Show:  Label: "labelFive" with labelString: "labelFive.one"
Show:  Label: "labelFive" with labelString: "labelFive.two"
Show:  Label: "labelFive" with labelString: "labelFive.three"
Show:  Label: "labelFive" with labelString: "labelFive.four"
Show:  Label: "labelFive" with labelString: "labelFive.five"

相反,我看到了:

Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"
Show:  Label: "labelFive" with labelString: "labelFive.five"

lambda 的参数绑定是在执行 lambda 时发生的,而不是在创建 lambda 时发生的。这至少是出乎意料的,我认为可以说是错误的。

我认为,尽管 lambda 是有限的,但它应该创建一个 CLOSURE——它的整个生命目的是在它创建时捕获它的参数状态,以便以后可以在 lambda 时使用它们被评估。这就是为什么它被称为“闭包”,因为它在创建时关闭了其参数的值。

我误会了什么?

【问题讨论】:

  • Python lambdas 是后期绑定。试试这个lambda someArg, ls=lambdaString: print(someArg, ls)
  • 正如@TheLazyScripter 提到的,lambdaString 的值在创建 oneArgLambda 时没有被评估,当你在循环中评估它时它会采用最新的值
  • 这不是特定于 lambda 的问题。一些相关讨论:docs.python-guide.org/writing/gotchas/#late-binding-closures

标签: python closures


【解决方案1】:

正如你所说,它创建了 CLOSURE,并且闭包在 lambdaString 的上层范围内使用声明的变量,但诀窍是你所有的 lambdas 都使用相同的 ref 到 lambdaString 因为你每次都会改变它,它会记住最后一个。例如:

c = ['one', 'two']
res = []

for i in range(2):
    for y in c:
        def la(x):
            print(x, y)
        res.append(la)

for la in res:
    la('Show: ')
# Show:  two
# Show:  two
# Show:  two
# Show:  two

你只需要另一个闭包来防止这种情况

c = ['one', 'two']
res = []

for i in range(2):
    for y in c:
        def closure_y(y):
            def la(x):
                print(x, y)
            return la
            
        res.append(closure_y(y))

for la in res:
    la('Show: ')
# Show:  one
# Show:  two
# Show:  one 
# Show:  two

完整的代码可能是

specification = {
    'labelOne': ['labelOne.one', 'labelOne.two', 'labelOne.three', 'labelOne.four', 'labelOne.five'],
    'labelTwo': ['labelTwo.one', 'labelTwo.two', 'labelTwo.three', 'labelTwo.four', 'labelTwo.five'],
    'labelThree': ['labelThree.one', 'labelThree.two', 'labelThree.three', 'labelThree.four', 'labelThree.five'],
    'labelFour': ['labelFour.one', 'labelFour.two', 'labelFour.three', 'labelFour.four', 'labelFour.five'],
    'labelFive': ['labelFive.one', 'labelFive.two', 'labelFive.three', 'labelFive.four', 'labelFive.five'],
    }

lambdas = []
for label, labelStrings in specification.items():
    for labelString in labelStrings:
        lambdaString = f"""Label: \"{label}\" with labelString: \"{labelString}\""""

        def clousure(lambdaString):
            oneArgLambda = lambda someArg: print(someArg, lambdaString)
            return oneArgLambda

        lambdas.append(clousure(lambdaString))

for each in lambdas:
    each('Show: ')

【讨论】:

  • 我接受了这个作为答案,因为它有效并且保留了我开始使用的结构。我正在为 Neo4J 的 Python 驱动程序编写一个适配器。这些字符串是为 69 个标签/属性名称对中的每一个创建索引的密码命令。每个 lambda 的参数是一个在“with”上下文中创建的“会话”。这种方法有效。我了解 Python 中 lambda 表达式的后期绑定。我认为这种行为是一个错误。如果它不会表现得像一个闭包,那么它应该有一个不同的名称。无论如何,感谢 Alex Solovyov 解决了我的问题。
【解决方案2】:

这是另一种选择

lambdas = []
for label, labelStrings in specification.items():
    for labelString in labelStrings:
        lambdaString = f"""Label: \"{label}\" with labelString: \"{labelString}\""""
        oneArgLambda = lambda someArg, lambdaString: print(someArg, lambdaString)
        lambdas.append((oneArgLambda, lambdaString))

for f, lambdaString in lambdas:
    f('Show: ', lambdaString)

【讨论】:

    【解决方案3】:

    你有一些其他的 cmets 和答案来解释发生了什么,以及你的 问题是“有趣的”是它迫使读者困惑的感觉 代码和各种绑定问题。

    但是,如果我在审核期间从同事那里看到您的代码,我会要求 重写——不是因为我会立即知道有一个错误,而是因为它 需要过多地考虑是否来自周围的绑定 (和不断变化的)范围的行为将完全符合预期。

    相反,坚持在你的程序中更严格的纪律,从而减轻 读者的认知负担(大多数时候)。具体来说,移动 函数创建到一个真正孤立的范围,并传递所有不同的 该函数创建器的输入。这种方法是可靠的,因为它要么 在第一次尝试或完全失败(如果你忽略通过所有 函数创建器所需的参数)。

    一种方法:

    # A function to create another function, with non-surprising argument binding.
    # We expect nothing from the surrounding scope. All business can be done locally.
    def get_func(label, x):
        return lambda prefix: print(f'''{prefix} => {label}: {x}''')
    
    # Some input data.
    specification = {
        label : [label + str(n) for n in range(3)]
        for label in ('A', 'B', 'C')
    }
    
    # Use that data to create some functions.
    funcs = [
        get_func(label, x)
        for label, xs in specification.items()
        for x in xs
    ]
    
    # Run 'em.
    for f in funcs:
        f('Show')
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2011-06-20
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 2018-11-04
      • 2021-09-22
      相关资源
      最近更新 更多