what is the difference for python between lambda and regular function?

前端 未结 5 2156
故里飘歌
故里飘歌 2020-11-29 09:10

I\'m curious about the difference between lambda function and a regular function (defined with def) - in the python level. (I know what is the diff

5条回答
  •  生来不讨喜
    2020-11-29 09:25

    They are the same type so they are treated the same way:

    >>> type(a)
    
    >>> type(b)
    
    

    Python also knows that b was defined as a lambda function and it sets that as function name:

    >>> a.func_name
    'a'
    >>> b.func_name
    ''
    

    In other words, it influences the name that the function will get but as far as Python is concerned, both are functions which means they can be mostly used in the same way. See mgilson's comment below for an important difference between functions and lambda functions regarding pickling.

提交回复
热议问题