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

前端 未结 5 2162
故里飘歌
故里飘歌 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:31

    lambda create anonymous function. This idea has been taken from functional programming languages. In this way you can create and pass the function to other functions like map and filter. ( look here )
    You can pass normal functions to these functions too, but since mostly their simple and they have used nowhere else, it's inconvenient to through to whole process of definfing a new function.

    As an example take a look at this :

    >>> a = [1, 2, 3, 4]
    >>> print map( lambda x : x*2 + 1, a )
    [3, 5, 7, 9, 11]
    

提交回复
热议问题