higher-order function | Python Glossary – Real Python

In programming, a higher-order function is a function that either takes one or more functions as arguments or returns a function as its result. This powerful feature allows you to create more abstract and flexible code.

Higher-order functions are a cornerstone of functional programming, enabling you to use functions like map(), filter(), and lambda functions to manipulate data in a clean and expressive way.

You can often replace loops with higher-order functions to write code that is more readable and concise. This can make your code more modular and maintainable.

Example

Here’s a quick example of using map(), which is a higher-order function in Python:

>>> def square(base):
...     return base**2
...

>>> list(map(square, [1, 2, 3, 4, 5]))
[1, 4, 9, 16, 25]

In this example, map() takes the square() function and applies it to each element in the provided list, resulting in a new list of square values.

For additional information on related topics, take a look at the following resources: