Higher Order Functions in Python (33/100 Days of Python)
In computer programming, higher-order functions are functions that take other functions as inputs or return functions as outputs. They are an essential part of functional programming and play an important role in modern programming languages, including Python.
What are Higher-Order Functions?
A higher-order function is a function that can take other functions as arguments and/or return functions as outputs. These functions allow you to write more concise, flexible, and abstract code. Higher-order functions are often used to create new functions from existing functions, abstract common patterns in code, and write more modular and reusable code.
For example, consider the following function that takes a function and an iterable as arguments and returns the result of applying the function to each element in the iterable:
def apply_to_each(func, iterable):
return [func(x) for x in iterable]
def square(x):
return x * x
numbers = [1, 2, 3, 4, 5]
squared_numbers = apply_to_each(square, numbers)
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Here, apply_to_each
is a higher-order function because it takes a function, square
, as an argument.
Common Higher-Order Functions in Python
There are several higher-order functions in Python that are commonly used, such as map
, filter
, and reduce
:
map
:map
takes a function and an iterable as arguments and returns a new iterable that is the result of applying the function to each element in the original iterable:
def square(x):
return x * x
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))
print(squared_numbers) # [1, 4, 9, 16, 25]
filter
:filter
takes a function and an iterable as arguments and returns a new iterable that contains only the elements from the original iterable for which the function returnsTrue
. For example:
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # [2, 4]
reduce
:reduce
takes a function and an iterable as arguments and returns a single value that is the result of reducing the iterable to a single value using the function. For example:
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(add, numbers)
print(sum_of_numbers) # 15
Advantages of Higher-Order Functions
Higher-order functions have several advantages over traditional functions in programming. One of the main advantages is that they allow you to write more abstract, flexible, and reusable code. Higher-order functions also make it easier to test and debug your code, as they help you break down complex problems into smaller, more manageable pieces.
Another advantage of higher-order functions is that they promote functional programming, a paradigm that emphasizes immutability, pure functions, and composability. By using higher-order functions, you can write code that is more modular, easy to reason about, and less prone to bugs.
Finally, higher-order functions allow you to write more concise code by eliminating repetitive or redundant code. You can use higher-order functions to abstract away common patterns in your code and make your code more readable and maintainable.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course — higher-order functions
- Full series: 100 Days of Python
- Previous topic: Lambda Functions
- Next topic: Working With Files