'lamba variable = variable:somefunction()'如何工作? (包括示例) [英] How does 'lamba variable=variable: somefunction()' work? (Example included)

查看:71
本文介绍了'lamba variable = variable:somefunction()'如何工作? (包括示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我偶然发现用户提交的解决方案(某人)时,我正在寻找StackOverflow来解决我的问题其他问题):


  appsMenu.add_command(label = app,command = openApp(Apps [app]))

调用函数的命令参数需要包装在 lambda 中,以防止他们立即被召唤。另外,绑定到for循环中的命令需要将循环变量作为默认参数,以便每次都绑定正确的值。

  appsMenu .add_command(label = app,command = lambda app = app:openApp(Apps [app]))


如您所见,用户编写了 lambda app = app ,出于对我的爱,我不知道为什么。

解决方案

lambda 表达式用于定义单行(匿名)函数。因此,写

  f = lambda x:2 * x 

(或多或少)等于

  def f(x ):
返回2 * x

此外,对于正常功能,可以在 lambda 函数中也具有默认参数。因此,

  k = 2 
f = lambda x = k:2 * x

可以称为 f()其中 x 将是 k 的值,即 2 f( 3),其中 x = 3 。现在,更奇怪的是,由于 lambda 函数具有其自己的名称空间,因此可以交换 k 其中 x

  x = 2 
f = lambda x = x:2 * x

则表示默认值 f()将是(外部) x 值(即 2 ),如果



因此,表达式

  lambda app = app:openApp(Apps [app])

是一个函数(未命名),需要一个参数 app 和(外部)变量 app 的默认值。并在调用时执行 openApp(Apps [app])指令,其中 Apps 是全局(外部)指令对象。



现在,我们为什么要这么做,而不是只写 command = openApp(Apps [app])调用 .add_command()时?原因是我们不希望在定义命令时(即在调用 .add_command()时)执行命令,而是在以后的时间(即菜单项为



因此,为了推迟执行,我们包装执行 openApp(Apps [app])在函数中。选择菜单项后,将执行此功能。因此,我们将调用 openApp ,而不是现在的 now (在我们要添加命令时),否则将是这种情况。 / p>

现在,由于调用 lambda 函数时不带任何参数,因此它将使用其默认参数(外部) app 作为(内部) app 。这使得整个参数有点多余,因为可以直接在 lambda 表达式中访问外部变量。因此,可以将表达式简化为 lambda:openApp(Apps [app])即不带任何参数,使整行

  appsMenu.add_command(label = app,command = lambda:openApp(Apps [app]))

对我来说,这看起来有点简单,这是我编写它的方式,但是从概念上讲,这与具有默认参数的代码没有区别。


I was searching StackOverflow for a solution to a problem of mine when I stumbled upon this user submitted solution (to someone else's question):

appsMenu.add_command(label=app, command=openApp(Apps[app]))

Command parameters that call functions need to be wrapped in a lambda, to prevent them from being called right away. Additionally, commands bound within a for loop need the looping variable as a default argument, in order for it to bind the right value each time.

 appsMenu.add_command(label=app, command=lambda app=app: openApp(Apps[app]))

As you can see, the user wrote lambda app=app, and for the love of me I cannot figure out why. What does it do?

解决方案

The lambda expression is used to define a one-line (anonymous) function. Thus, writing

f = lambda x: 2*x

is (more or less) equivalent to

def f(x):
    return 2*x

Further, as for normal functions, it is possible to have default arguments also in a lambda function. Thus

k = 2
f = lambda x=k: 2*x

can be called either as f() where x will be the value of k i.e. 2 or as f(3) where x = 3. Now, to make it even more strange, since the lambda function has its own name space, it is possible to exchange the k with x as

x = 2
f = lambda x=x: 2*x

which then means that the default value of f() will be the (outer) x value (i.e. 2) if no other parameter is set.

Thus, the expression

lambda app=app: openApp(Apps[app])

is a function (unnamed) that takes one argument app with the default value of the (outer) variable app. And when called, it will execute the instruction openApp(Apps[app]) where Apps is a global (outer) object.

Now, why would we want that, instead of just writing command=openApp(Apps[app]) when calling .add_command()? The reason is that we want the command to execute not when defining the command, i.e., when calling .add_command(), but at a later time, namely when the menu item is selected.

Thus, to defer to the execution we wrap the execution of openApp(Apps[app]) in a function. This function will then be executed when the menu item is selected. Thereby we will call openApp then, instead of right now (at the time we want to add the command) which would be the case otherwise.

Now, since the lambda function is called with no arguments, it will use its default parameter (outer) app as (inner) app. That makes the whole argument a bit redundant, as the outer variable is directly accessible within the lambda expression. Therefore, it is possible to simplify the expression to lambda: openApp(Apps[app]) i.e. wiht no arguments, making the full line

appsMenu.add_command(label=app, command=lambda: openApp(Apps[app]))

To me, this looks a bit simpler, and is the way I would write it, but conceptually there is no difference between this and the code with default parameters.

这篇关于'lamba variable = variable:somefunction()'如何工作? (包括示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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