Python Lambda Function Tutorial

In this section, we will learn what Lambda Function is and how to use it in Python.

What is Lambda in Python? (Python Anonymous Function)

First of all, an anonymous function is a function that does not have a name!

Anonymous functions are mainly used in places where we need a function and want to create one on the fly to do the work.

For example, let’s say we have a function that needs another function as its argument! But we don’t have the required function in our program! So, using the anonymous function, we can create one right where the target function is being called and pass it as its argument.

However, be aware that using the typical method of creating a function, we can’t create an anonymous function! We need another method which is called lambda!

Python Lambda Declaration

This is how we create a lambda function (AKA anonymous function):

lambda parameters: expression

`lambda`: first we start with the keyword lambda in order to create a lambda function.

`parameters`: after that comes the parameters of the target anonymous function. Here we put as many parameters as our lambda function needs.

Note: There’s no need for parentheses. Also, parameters are separated using comma `,`. if the lambda function does not take any argument, then put an empty space as the replacement of the parameters.

`:`: after the parameters, we separate the body of the anonymous function from its parameters using a colon in between.

`expression`: You should know that within the body of an anonymous function we can only put one expression!

Also, an anonymous function can be passed as the value to a variable!

Example: lambda expression in Python

def createAnonyFunction():
    return lambda : print("Hello World")

res = createAnonyFunction()

res()

Output:

Hello World

Here within the body of the createAnonyFunction() function, we’ve created a lambda function and returned that as the result of this function.

Now, because the `res` variable is pointing to this lambda function, we could use the variable and call the function.

That’s how we got the message `Hello World` on the output stream.

Python Lambda Example:

def createAnonyFunction():
    return lambda fName,lName : print(f"Hello {fName} {lName}")

res = createAnonyFunction()

res("John","Doe")

def iNeedFunction(func):
    func("Omid","Dehghan")

iNeedFunction(res)

Output:

John Doe

Omid Dehghan

Here, the createAnonyFunction() function returns a lambda function that takes two arguments and print those arguments to the output stream.

This lambda function is passed, then two the `res` variable as well as the argument of the `iNeedFunction` function.

As you can see, both of them passed two arguments to the lambda expression when the call happened! If they didn’t, an error would’ve been occurred.

Note: Python Closure works the same for lambda functions.

Example: lambda function and Python Closure

fullName = "Omid Dehghan"

def createAnonyFunction():
    fullName = "John Doe"
    return lambda fName,lName : print(f"Hello {fullName}")

res = createAnonyFunction()

res("John","Doe")

def iNeedFunction(func):
    func("Omid","Dehghan")

iNeedFunction(res)

Output:

Hello John Doe

Hello John Doe

Note: please check the Python Closure section if you’re not familiar with this concept.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies