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:
- Python's map(): Processing Iterables Without a Loop (Tutorial)
- Python's filter(): Extract Values From Iterables (Tutorial)
- Python's reduce(): From Functional to Pythonic Style (Tutorial)
- Defining Your Own Python Function (Tutorial)
- The Python return Statement: Usage and Best Practices (Tutorial)
- Using Functional Programming in Python (Course)
- Functional Programming in Python: When and How to Use It (Quiz)
- Python's map() Function: Transforming Iterables (Course)
- Filtering Iterables With Python (Course)
- Defining and Calling Python Functions (Course)
- Defining and Calling Python Functions (Quiz)
- Defining Your Own Python Function (Quiz)
- Using the Python return Statement Effectively (Course)
- The Python return Statement (Quiz)