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

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

    First consider the diff b/w the two.

    Lambda functions: are operator can have any number of arguments, but it can have only one expression. It cannot contain any statements and it returns a function object which can be assigned to any variable. They can be used in the block they were created.

    def functions: Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organised and manageable. They can be called and used anywhere we want.

    Here you can get more clear difference by following example.

    Defining a function

        def add(a,b):
          return a+b
        print(add(4,5))
    

    Defining a lambda

        add = lambda x, y : x + y 
        print(add(4,5))
    

提交回复
热议问题