Python方法与lambd的区别

2024-04-26 22:47:30 发布

您现在位置:Python中文网/ 问答频道 /正文

方法和lambda之间可能存在哪些差异?我有一个身份函数,不知道它有多大不同,我如何定义它?你知道吗

def ident(x): return x
ident = lambda x: x

它们在功能上肯定是相同的,但它们在性能上还是在其他方面有所不同?你知道吗


Tags: 方法lambda函数功能return定义def身份
1条回答
网友
1楼 · 发布于 2024-04-26 22:47:30

两者的作用相同,但都不是方法;第一个是函数,第二个是匿名函数,通过将其赋给变量来命名。你知道吗

评论:

performance differences are negligible, but more importantly, lambda functions only support expressions, where's def statements can have any combination of complex statements in their body. The only purpose of lambda is for convenience, usually as an argument to a higher order function, and if you assign lambda functions to a name, that negates their only use case. Indeed, although it is allowed, it is explicitly recommended against by PEP8 style guidelines.
~ juanpa.arrivillaga

相关问题 更多 >