What is a lambda (function)?

后端 未结 23 2780
太阳男子
太阳男子 2020-11-22 04:47

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

23条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 05:20

    Lambda explained for everyone:

    Lambda is an annonymous function. This means lambda is a function objekt in Python that doesnt require a reference before. Let's concider this bit of code here:

    def name_of_func():
        #command/instruction
        print('hello')
    
    print(type(name_of_func))   #the name of the function is a reference
                                #the reference contains a function Objekt with command/instruction
    

    To proof my proposition I print out the type of name_of_func which returns us:

    
    

    A function must have a interface, but a interface dosent needs to contain something. What does this mean? Let's look a little bit closer to our function and we may notice that out of the name of the functions there are some more details we need to explain to understand what a function is.

    A regular function will be defined with the syntax "def", then we type in the name and settle the interface with "()" and ending our definition by the syntax ":". Now we enter the functions body with our instructions/commands.

    So let's consider this bit of code here:

    def print_my_argument(x):
        print(x)
    
    
    print_my_argument('Hello')
    

    In this case we run our function, named "print_my_argument" and passing a parameter/argument through the interface. The Output will be:

    Hello
    

    So now that we know what a function is and how the architecture works for a function, we can take a look to an annonymous function. Let's consicder this bit of code here:

    def name_of_func():
        print('Hello')
    
    
    
    lambda: print('Hello')
    

    these function objekts are pretty much the same except of the fact that the upper, regular function have a name and the other function is an annonymous one. Let's take a closer look on our annonymous function, to understand how to use it.

    So let's concider this bit of code here:

    def delete_last_char(arg1=None):
        print(arg1[:-1])
    
    string = 'Hello World'
    delete_last_char(string)
    
    f = lambda arg1=None: print(arg1[:-1])
    f(string)
    

    So what we have done in the above code is to write once againg, a regular function and an anonymous function. Our anonymous function we had assignd to a var, which is pretty much the same as to give this function a name. Anyway, the output will be:

    Hello Worl
    Hello Worl
    

    To fully proof that lambda is a function object and doesnt just mimik a function we run this bit of code here:

    string = 'Hello World'
    f = lambda arg1=string: print(arg1[:-1])
    f()
    print(type(f))
    

    and the Output will be:

    Hello Worl
    
    

    Last but not least you should know that every function in python needs to return something. If nothing is defined in the body of the function, None will be returned by default. look at this bit of code here:

    def delete_last_char(arg1):
        print(arg1[:-1])
    
    string = 'Hello World'
    x = delete_last_char(string)
    
    f = lambda arg1=string: print(arg1[:-1])
    x2 = f()
    
    print(x)
    print(x2)
    

    Output will be:

    Hello Worl
    Hello Worl
    None
    None
    

提交回复
热议问题