Lambda functions and anonymous functions in Python

by Alex
Lambda functions and anonymous functions in Python

As you already know, the keyword def is used to define standard functions in Python. But in addition to these standard functions, Python also has so-called anonymous or lambda functions. The keyword lambda is used to create them. Usually such a function is not intended to be reused. Syntax:
lambda [arguments] : expression
A lambda function can have zero or more arguments before the ‘:’ symbol. When such a function is called, the expression specified after ‘:’ is executed. An example definition of a lambda function:
get_cube = lambda x : x ** 3
The above lambda function starts with the keyword lambda followed by the parameter x. The expression x ** 3 after ‘:’ returns the value of the cube of the number passed to the calling code. The lambda function itself lambda x : x ** 3 is assigned to the variable get_cube for its subsequent call as a named function.

The name of the variable becomes the name of the function so that we can work with it as a normal function. An example of a lambda function call:
>>> get_cube(4)
64
The above definition of a lambda function is similar to the following standard function:
def get_cube(x):
return x ** 3
The expression does not have to always return a value. The following lambda function returns nothing. An example of a lambda function that does not return a value:
>>> welcome = lambda user: print('Welcome, ' + name + '!')
>>> welcome('Anon')
Welcome, Anon!
Note: A lambda function can only have one expression. Obviously, it cannot replace a function whose body contains conditions, loops, etc. The following lambda function contains several parameters. An example of a lambda function with three parameters:
>>> get_prod = lambda a, b, c : a * b * c
>>> get_prod(3, 5, 7)
105
The lambda function can also take any number of parameters. An example of a lambda-function with an undefined number of arguments (only the first 3 are used):

Lambda function without parameters

Below is an example of a lambda function without parameters.
>>> welcome = lambda : print('Welcome!')
>>> welcome()
>>> Welcome!

Anonymous function

We can declare a lambda function and call it as an anonymous function without assigning it to a variable. An example of an anonymous lambda function:
>>> (lambda x: x**3)(10)
1000
Here lambda x: x3 defines an anonymous function and calls it once, passing arguments in parentheses (lambda x: x3)(10). In Python, functions, like literals, can be passed as arguments.

Lambda functions are especially useful when we want to send a function as input to another function. We can pass an anonymous lambda function, without assigning it to a variable, as an argument to another function. An example of passing a lambda function as a parameter:
def run_task(task):
print('Before running the task')
task()
print('After running the task')
run_task(lambda : print('Task is complete!')) # passing an anonymous function
important_task = lambda : print('Important task is complete!')
run_task(important_task) # pass the lambda function
Output:
Before running the task
Task is complete!
After running the task
Before running the task
Important task is complete!
After running the task
The run_task() function shown above is defined with a task parameter, which is called as a function within run_task(). run_task(lambda : print(‘Task is complete!’)) calls run_task() with an anonymous lambda function as an argument. Python has built-in functions that take other functions as arguments.

The map(), filter(), and reduce() functions are important functional programming tools. All of them take a function as input. Such an argument function can be a regular function or a lambda function. An example of passing a lambda function to map():
>>> prime_cube_list = map(lambda x: x**3, [2, 3, 5, 7, 11]) # passing an anonymous function
>>> next(prime_cube_list)
8
>>> next(prime_cube_list)
27
>>> next(prime_cube_list)
125
>>> next(prime_cube_list)
343
>>> next(prime_cube_list)
1331

Related Posts

LEAVE A COMMENT