函数定义与Lambda表达式

2024-03-28 20:33:08 发布

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

给出这两个片段:

def sex (code):
    return {'m': 'masculino', 'f': 'femenino', '': 'ignorado'} [code]

以及

^{pr2}$

实际的区别是什么?他们有不同的行为吗?在

最重要的是:一个比另一个好吗?在

我问这个问题,是因为我在这个论坛(python3 - learning about searching, this very simple example does not work right)中的一个答案提供了两种可能性,但被另一个用户截短(“修改”),删除了lambda术语。修改的原因是最后一个选项(lambda表达式)比函数定义更差。这让我想知道:

为什么lambda表达式比函数定义差?尤其是像这样简单的。在


Tags: lambda函数return定义表达式defcode论坛
3条回答

除了BrenBarn's excellent answer中提供的信息外,您会发现许多Python程序员坚持总是使用def而不是{},因此我将尝试解释为什么会这样。在

基本上可以从Zen of Python归结为以下一行:

There should be one-- and preferably only one --obvious way to do it.

这在Guido van Rossum's post about this issue中引用(Guido是Python的创建者):

Why drop lambda? Most Python users are unfamiliar with Lisp or Scheme, so the name is confusing; also, there is a widespread misunderstanding that lambda can do things that a nested function can't -- I still recall Laura Creighton's Aha!-erlebnis after I showed her there was no difference! Even with a better name, I think having the two choices side-by-side just requires programmers to think about making a choice that's irrelevant for their program; not having the choice streamlines the thought process. Also, once map(), filter() and reduce() are gone, there aren't a whole lot of places where you really need to write very short local functions; Tkinter callbacks come to mind, but I find that more often than not the callbacks should be methods of some state-carrying object anyway (the exception being toy programs).

lambda和返回相同内容的def之间没有函数上的区别。它们完全相同,具有相同的字节码。在

>>> def sex (code):
...     return {'m': 'masculino', 'f': 'femenino', '': 'ignorado'} [code]
>>> sex2 = lambda code: {'m': 'masculino', 'f': 'femenino', '': 'ignorado'} [code]
>>> sex.__code__.co_code == sex2.__code__.co_code
True

我要说,从一般意义上说,任何一种编写函数的方式都是“糟糕”的,这是错误的。但是,在您的示例中,def版本更好。原因是,一般来说lambdas的目的是在表达式中简洁地编写简短的匿名函数。做name = lambda: ...没有实际意义。如果你要立即给一个匿名函数取个名字,就没有理由定义它。在

lambdas的重点是您可以做someFuncRequiringCallback(lambda x: x+1),在这种情况下,您不想给函数起一个名字,也不想将它用于除此之外的任何事情。如果您想跨多个上下文使用一个函数,只需用一个正则的def来定义它。在

它们是一样的,检查反汇编的python字节码

>>> def sex1 (code):
    return {'m': 'masculino', 'f': 'femenino', '': 'ignorado'} [code]

>>> sex2 = lambda code: {'m': 'masculino', 'f': 'femenino', '': 'ignorado'} [code]
>>> dis.dis(sex1)
  2           0 BUILD_MAP                3
              3 LOAD_CONST               1 ('masculino')
              6 LOAD_CONST               2 ('m')
              9 STORE_MAP           
             10 LOAD_CONST               3 ('femenino')
             13 LOAD_CONST               4 ('f')
             16 STORE_MAP           
             17 LOAD_CONST               5 ('ignorado')
             20 LOAD_CONST               6 ('')
             23 STORE_MAP           
             24 LOAD_FAST                0 (code)
             27 BINARY_SUBSCR       
             28 RETURN_VALUE        
>>> dis.dis(sex2)
  1           0 BUILD_MAP                3
              3 LOAD_CONST               1 ('masculino')
              6 LOAD_CONST               2 ('m')
              9 STORE_MAP           
             10 LOAD_CONST               3 ('femenino')
             13 LOAD_CONST               4 ('f')
             16 STORE_MAP           
             17 LOAD_CONST               5 ('ignorado')
             20 LOAD_CONST               6 ('')
             23 STORE_MAP           
             24 LOAD_FAST                0 (code)
             27 BINARY_SUBSCR       
             28 RETURN_VALUE    

甚至两个都有相同的类型

^{pr2}$

相关问题 更多 >