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

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

    The only difference is that (a) the body of a lambda can consist of only a single expression, the result of which is returned from the function created and (b) a lambda expression is an expression which evaluates to a function object, while a def statement has no value, and creates a function object and binds it to a name.

    In all other material respects they result in identical objects - the same scope and capture rules apply. (Immaterial differences are that lambda-created functions have a default func_name of "". This may affect operation in esoteric cases - e.g. attempts to pickle functions.).

提交回复
热议问题