This material is optional. You are not expected to know this for a test and there are no labs or projects using this material.
Lambda functions
This guide introduces you to the concept of lambda functions.
Passing a function as an argument
Consider the following example:
def get_full_name(first_name, last_name, formatter):
return formatter(first_name, last_name)
The function get_full_name()
takes three parameters — a first name, a last
name, and a function that formats the first and last name. Python allows you
to pass one function as a parameter to a second function.
In the above example, the only requirement for a formatter()
function is that
it needs to take two string arguments, representing a first and last name. Here
are some examples of potential formatter()
functions:
def first_last(first_name, last_name):
return f"{first_name} {last_name}"
def last_first(first_name, last_name):
return f"{last_name}, {first_name}"
Imagine that you have first_name = 'Kermit'
and last_name = 'The Frog'
.
Notice that the first function prints Kermit The Frog
and the second function
prints The Frog, Kermit
.
Here is a full program to use this idea:
def get_full_name(first_name, last_name, formatter):
return formatter(first_name, last_name)
def first_last(first_name, last_name):
return f"{first_name} {last_name}"
def last_first(first_name, last_name):
return f"{last_name}, {first_name}"
if __name__ == '__main__':
full_name = get_full_name('Kermit', 'The Frog', first_last)
print(full_name) # Kermit The Frog
full_name = get_full_name('Kermit', 'The Frog', last_first)
print(full_name) # The Frog, Kermit
lambda function
Lambda functions enable us to do the same thing but in a simpler way:
def get_full_name(first_name, last_name, formatter):
return formatter(first_name, last_name)
if __name__ == '__main__':
full_name = get_full_name(
'Kermit',
'The Frog',
lambda first_name, last_name: f"{first_name} {last_name}"
)
print(full_name)
full_name = get_full_name(
'Kermit',
'The Frog',
lambda first_name, last_name: f"{last_name}, {first_name}"
)
print(full_name)
A lambda function is an anonymous function. It has no name. In the above
example, it is created and passed as a parameter at the same time, using the
lambda
keyword
A lambda function can have only one expression in the body of the function. For this reason, it is often called a lambda expression.
Storing a function in a variable
Let’s say we have defined a complete function as a formatter()
:
def first_last(first_name, last_name):
return f"{first_name} {last_name}"
We can set this equal to a variable:
formatter = first_last
Then we can use the formatter as before:
if __name__ == '__main__':
formatter = first_last
full_name = get_full_name('Kermit', 'The Frog', formatter)
print(full_name) # Kermit The Frog
formatter = first_last
full_name = get_full_name('Kermit', 'The Frog', formatter)
print(full_name) # The Frog, Kermit
This example is not particularly useful, because it is an extra step to store the function in a variable. But there may be other cases where this is useful.
You can likewise store a lambda function in a variable:
if __name__ == '__main__':
formatter = lambda first_name, last_name: f"{first_name} {last_name}
full_name = get_full_name('Kermit', 'The Frog', formatter)
print(full_name) # Kermit The Frog
formatter = lambda first_name, last_name: f"{last_name}, {first_name}
full_name = get_full_name('Kermit', 'The Frog', formatter)
print(full_name) # The Frog, Kermit
Again, this is not particularly useful, because it takes an extra step. However, this illustrates the concept that a function (a full one or a lambda expression) is treated like any other Python data and can be stored in a variable and passed as a parameter. This means Python supports first-class functions, which is an important programming language concept.