Background
The concept of higher-order functions comes from functional programming and has been available in Python from the very beginning. It allows writing more abstract, flexible, and concise code.
The Problem
Many underestimate the capabilities of functions as first-class objects. Errors arise from incorrect passing/returning of functions, incorrect calls, or loss of code readability.
The Solution
In Python, a function is a full-fledged object. A function can be passed as an argument, returned from other functions, and stored in collections. This is what is referred to as a higher-order function. This enables the implementation of universal wrappers, callbacks, decorators, and a number of other patterns.
Code example:
def apply_function(func, value): return func(value) def square(x): return x * x result = apply_function(square, 5) # Returns 25
Key Features:
Can higher-order functions return not only functions but also data?
Yes, a higher-order function is any function that takes a function as an argument and/or returns a function. There is no restriction on the return value.
Does the built-in function map return a generator object or the list itself? (Python 3+)
In Python 3, map returns an iterable generator object, not a list. To get a list, you need to explicitly wrap it in list(map(...)).
squared = map(lambda x: x**2, [1,2,3]) result = list(squared) # Now we get [1, 4, 9]
Is it possible to assign functions to variables and store them in collections?
Yes, functions can be assigned to variables, can be stored in lists, dictionaries, and passed as arguments just like values. They are ordinary Python objects.
def greet(): print('Hello') a = greet a() # Will call greet
Attempting to implement logging for multiple functions without higher-order functions led to duplicated code and errors in each individual handler.
Pros:
Cons:
Introduced a universal logging decorator — all functions started to be logged uniformly, code became cleaner.
Pros:
Cons: