循环中的Python Lambda [英] Python Lambda in a loop

查看:398
本文介绍了循环中的Python Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码段:

# directorys == {'login': <object at ...>, 'home': <object at ...>}
for d in directorys:
    self.command["cd " + d] = (lambda : self.root.change_directory(d))

我希望创建一个具有以下两个功能的字典:

I expect to create a dictionary of two function as following :

# Expected :
self.command == {
    "cd login": lambda: self.root.change_directory("login"),
    "cd home": lambda: self.root.change_directory("home")
}

但是看起来生成的两个lambda函数是完全相同的:

but it looks like the two lambda function generated are exactly the same :

# Result :
self.command == {
    "cd login": lambda: self.root.change_directory("login"),
    "cd home": lambda: self.root.change_directory("login")   # <- Why login ?
}

我真的不明白为什么.您有什么建议吗?

I really don't understand why. Do you have any suggestions ?

推荐答案

您需要为创建的每个函数绑定d.一种方法是将其作为具有默认值的参数传递:

You need to bind d for each function created. One way to do that is to pass it as a parameter with a default value:

lambda d=d: self.root.change_directory(d)

现在,函数中的d即使其名称相同,也使用该参数,并且在创建函数时将评估该参数的默认值.为帮助您查看此信息:

Now the d inside the function uses the parameter, even though it has the same name, and the default value for that is evaluated when the function is created. To help you see this:

lambda bound_d=d: self.root.change_directory(bound_d)

请记住默认值是如何工作的,例如对于可变对象(如列表和字典),因为您要绑定对象.

Remember how default values work, such as for mutable objects like lists and dicts, because you are binding an object.

这种带有默认值的参数惯用法已经足够普遍了,但是如果您对函数参数进行内省并根据其存在来确定要做什么,则可能会失败.您可以使用另一个闭包来避免使用该参数:

This idiom of parameters with default values is common enough, but may fail if you introspect function parameters and determine what to do based on their presence. You can avoid the parameter with another closure:

(lambda d=d: lambda: self.root.change_directory(d))()
# or
(lambda d: lambda: self.root.change_directory(d))(d)

这篇关于循环中的Python Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆