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

前端 未结 5 2124
故里飘歌
故里飘歌 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条回答
  •  -上瘾入骨i
    2020-11-29 09:31

    Lambda is an inline function where we can do any functionality without a function name. It is helpful when we use it as an argument to a higher-order function. Eg: A function that takes in other functions as arguments.

    Example of Function definition:

    >>> def func(a, b):
        return a * b
    
    >>> func(2,3)
    6
    >>> type(func)
    
    >>> func
    
    

    Example of Lambda expression:

    >>> multiply = lambda a, b: a * b
    >>> multiply(2, 3)
    6
    >>> type(multiply)
    
    >>> multiply
     at 0x034B6ED0>
    

    Both returns same output value. Only object returned are different. "func" name for Function and for Lambda.

提交回复
热议问题