【发布时间】: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