您可以在lambda中分配变量吗? [英] Can you assign variables in a lambda?

查看:82
本文介绍了您可以在lambda中分配变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时使用lambda语句执行数学运算,并且碰巧反复使用一个特定值.因此,我想知道是否可以在lambda语句中分配和使用变量.

I was using a lambda statement to perform math, and happened to repeatedly use one certain value. Therefore I was wondering if it was possible to assign and use a variable within a lambda statement.

我尝试过类似的事情:

a = lambda n:(b=3+2*n) #[my math here]

但是这只会引发错误,我想知道是否有一种方法可以做到这一点.

However this just raises errors, and I was wondering if there was a way to do this.

推荐答案

不,您不能.仅 lambda

Nope, you can't. Only expressions allowed in lambda:

lambda_expr        ::=  "lambda" [parameter_list]: expression
lambda_expr_nocond ::=  "lambda" [parameter_list]: expression_nocond

但是,您可以在lambda的内部定义 second lambda 并立即使用所需的参数进行调用. (这是否真的更好,可能是另一个问题.)

You could, however, define a second lambda inside the lambda and immediately call it with the parameter you want. (Whether that's really better might be another question.)

>>> a = lambda n: ((3+2*n), n*(3+2*n))  # for reference, with repetition
>>> a(42)
(87, 3654)
>>> a2 = lambda n: (lambda b: (b, n*b))(3+2*n)  # lambda inside lambda
>>> a2(42)
(87, 3654)
>>> a3 = lambda n: (lambda b=3+2*n: (b, n*b))()  # using default parameter
>>> a3(42)
(87, 3654)

当然,外部和内部lambda都可以具有多个参数,即,您可以一次定义多个变量".这种方法相对于(例如)定义第一个lambda 以外的另一个好处是,您仍然可以使用原始参数(如果您使用b pre-调用了a计算出的),您只需对b进行一次计算(除了重复调用a中的b的函数).

Of course, both the outer and the inner lambda can have more than one parameter, i.e. you can define multiple "variables" at once. The benefit of this approach over, e.g., defining a second lambda outside of the first is, that you can still also use the original parameters (not possible if you invoked a with b pre-calculated) and you have to do the calculation for b only once (other than repeatedly invoking a function for the calculation of b within a).

此外,受链接问题的最佳答案的启发,您还可以定义一个或多个变量作为该变量的一部分lambda中的列表推导或生成器,然后从该生成器或列表获取next(第一个也是唯一的)结果:

Also, inspired by the top answer to the linked question, you could also define one or more variables as part of a list comprehension or generator within the lambda, and then get the next (first and only) result from that generator or list:

>>> a4 = lambda n: next((b, n*b) for b in [3+2*n])
>>> a4(42)
(87, 3654)

但是,我认为lambda-in-a-lambda背后的意图更加清晰.最后,请记住,除了单行lambda之外,您还可以使用更清晰的三行def语句...

However, I think the intent behind the lambda-in-a-lambda is a bit clearer. Finally, keep in mind that instead of a one-line lambda, you could also just use a much clearer three-line def statement...

此外,从Python 3.8开始,将出现赋值表达式应该应该可以编写这样的内容. (请注意,由于我还没有Python 3.8,因此无法尝试/验证.)

Also, starting with Python 3.8, there will be assignment expressions, which should make it possible to write something like this. (Note that I could not try/verify this as I do not have Python 3.8 yet.)

>>> a5 = lambda n: ((b := 3+2*n), n*b))

这篇关于您可以在lambda中分配变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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