向我解释这个功能,lambda混乱 [英] explain this function to me, lambda confusion

查看:70
本文介绍了向我解释这个功能,lambda混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对lambda有一个粗略的了解,但到目前为止只发现使用

一次(在tkinter中传递lambda作为参数我可以

规避一些棘手的事情东西)。

以下功能有什么意义?


def addn(n):

返回lambda x, inc = n:x + inc


如果我确实addn(5)它返回

i have a rough understanding of lambda but so far only have found use
for it once(in tkinter when passing lambda as an argument i could
circumvent some tricky stuff).
what is the point of the following function?

def addn(n):
return lambda x,inc=n: x+inc

if i do addn(5) it returns


>> def addn(n):
>>def addn(n):



返回lambda x,inc = n:x + inc

return lambda x,inc=n: x+inc


>> addn (5)
>>addn(5)



< function< lambdaat 0x01D81830>

好​​吗?所以我写什么才能让它真正做点什么呢。并且是

inc = n必要的我不能做x + n?

<function <lambdaat 0x01D81830>
ok? so what do i write to make it actually do something. and is the
inc=n necessary i cant do x+n?

推荐答案

以及这里的差异:


g = lambda x = 5:x * x

g = lambda x:x * x


第一个这是一个错误的写,但它工作

和x = 5似乎完全被忽略。为什么?它对

all无效?
and what si the diffrence here:

g = lambda x=5:x*x
g = lambda x:x*x

the first was a mistake to write but it worked
and the x=5 seems to be completely ignored. why? it has no effect at
all?


On 7 Maj,23:47,globalrev< skanem ... @ yahoo.sewrote:
On 7 Maj, 23:47, globalrev <skanem...@yahoo.sewrote:

以及这里的差异:


g = lambda x = 5:x * x

g = lambda x:x * x


第一个是写错了但是它工作了

而且x = 5似乎完全被忽略了。为什么?它对

都没有影响吗?
and what si the diffrence here:

g = lambda x=5:x*x
g = lambda x:x*x

the first was a mistake to write but it worked
and the x=5 seems to be completely ignored. why? it has no effect at
all?



啊等等我现在看到它有默认的那种。 g()返回25而

g(8)返回64.

ah wait now i see it has a default kind of then. g() returns 25 while
g(8) returns 64.


On 7 mai,23:38,globalrev< skanem ... @ yahoo.sewrote:
On 7 mai, 23:38, globalrev <skanem...@yahoo.sewrote:

i对lambda有一个粗略的了解,但到目前为止只发现使用

一次(在tkinter传递时) lambda作为一个参数我可以

绕过一些棘手的东西。)

以下函数的重点是什么?


def addn(n):

返回lambda x,inc = n:x + inc
i have a rough understanding of lambda but so far only have found use
for it once(in tkinter when passing lambda as an argument i could
circumvent some tricky stuff).
what is the point of the following function?

def addn(n):
return lambda x,inc=n: x+inc



它返回一个接受一个参数并返回的函数结果

添加了这个参数,并将参数传递给了addn。


FWIW,Python的lambda只是创建一个非常快捷的捷径简单的

函数,上面的代码规范地写成:


def makeadder(n):

def adder(x ):

返回n + x

返回加法器

It returns a function that accept one argument and return the result
of the addition of this argument with the argument passed to addn.

FWIW, Python''s lambda is just a shortcut to create a very simple
function, and the above code is canonically written as:

def makeadder(n):
def adder(x):
return n + x
return adder


如果我确实添加(5)它返回
if i do addn(5) it returns



(剪辑)

(snip)


<函数< lambdaat 0x01D81830>


好​​吗?所以我写什么才能让它真正做点什么呢。
<function <lambdaat 0x01D81830>

ok? so what do i write to make it actually do something.



add5 = addn(5)

add5(1)

= 6

add5(2)

= 7


add42 = addn(42)

add42(1)

= 43

add5 = addn(5)
add5(1)
=6
add5(2)
=7

add42 = addn(42)
add42(1)
=43


并且是

inc = n必须做x + n吗?
and is the
inc=n necessary i cant do x+n?



在这种情况下,它不是。这个版本完全相同的事情

AFAICT:


def addn(n):

返回lambda x:x + n

In this case, it''s not. This version does exactly the same thing
AFAICT:

def addn(n):
return lambda x: x+n


这篇关于向我解释这个功能,lambda混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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