Understanding Higher-Order Functions in Python
This article explains Python's higher-order functions, covering their definition, how they can accept or return other functions, built-in examples like map, filter, reduce, and demonstrates custom higher-order functions, closures, and decorators with practical code snippets.
In Python, functions can encapsulate logic, be passed as arguments, returned as values, and even be nested. Higher-Order Functions are functions that can receive other functions as parameters or return functions. Mastering them makes code more flexible, concise, and abstract.
1. What is a Higher-Order Function?
A higher-order function satisfies at least one of the following conditions:
Accepts one or more functions as arguments.
Returns a function.
Python provides many built-in higher-order functions such as map() , filter() , and reduce() , and you can also define your own.
2. Functions as Arguments
You can pass a function as an argument to another function to achieve more flexible programming.
Example: Custom Higher-Order Function
def apply_operation(func, x, y):
return func(x, y)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
print(apply_operation(add, 3, 5)) # Output: 8
print(apply_operation(multiply, 3, 5)) # Output: 15Here, apply_operation receives a function func and calls it with x and y to compute the result.
3. Built-in Higher-Order Functions
Python offers several common higher-order functions that simplify data processing.
(1) map(function, iterable)
Applies a function to each element of an iterable and returns an iterator.
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
print(list(squared)) # Output: [1, 4, 9, 16, 25](2) filter(function, iterable)
Filters an iterable, keeping only elements that satisfy the condition.
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4, 6](3) reduce(function, iterable)
(Requires from functools import reduce ) Performs cumulative computation on an iterable.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_result = reduce(lambda x, y: x + y, numbers)
print(sum_result) # Output: 154. Functions as Return Values (Closures)
Higher-order functions can also return a function, a pattern commonly used for closures.
Example: Counter Function
def make_counter():
count = 0
def counter():
nonlocal count # modify outer variable
count += 1
return count
return counter
counter = make_counter()
print(counter()) # Output: 1
print(counter()) # Output: 2
print(counter()) # Output: 3Here, make_counter returns the inner function counter , which remembers and modifies the outer variable count .
5. Decorators – Typical Use of Higher-Order Functions
A decorator is essentially a higher-order function that takes a function and returns an enhanced version.
Example: Measuring Function Execution Time
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} execution time: {end_time - start_time:.4f} seconds")
return result
return wrapper
@timer
def long_running_function():
time.sleep(2)
long_running_function() # Output: long_running_function execution time: 2.0002 secondsThe @timer syntax is syntactic sugar for long_running_function = timer(long_running_function) .
6. Summary
Higher-order functions can accept functions as arguments or return functions.
map , filter , and reduce are built-in higher-order functions that simplify data handling.
Closures allow a function to remember and access variables from its enclosing scope.
Decorators are a typical application of higher-order functions, enabling function enhancement without modifying the original code.
Mastering higher-order functions lets you write more concise and flexible Python code, an essential skill for advanced Python development.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.