Python Higher Order Functions
Python treats functions as first-class objects, which means functions can be:
- Assigned to variables
- Passed as arguments
- Returned from other functions
- Stored in data structures
This capability enables the use of Higher Order Functions.
Higher-order functions are an important concept in functional programming and help developers write cleaner, more reusable code.
What is a Higher Order Function?
A Higher Order Function is a function that:
- Accepts one or more functions as arguments, or
- Returns a function as its result
Some functions do both.
Why Use Higher Order Functions?
They help:
- Reduce code duplication
- Improve readability
- Increase flexibility
- Support functional programming
- Create reusable code
Functions Are First-Class Objects
In Python, functions can be assigned to variables.
def greet():
return "Hello"
message = greet
print(message())Output
HelloPassing Functions as Arguments
A function can accept another function as a parameter.
def greet():
return "Hello"
def display(func):
print(func())
display(greet)Output
HelloUnderstanding the Flow
greet()
↓
display(greet)
↓
HelloReturning Functions
A higher-order function can return another function.
def outer():
def inner():
print("Inner Function")
return inner
result = outer()
result()Output
Inner FunctionExample: Function Factory
def multiply_by(n):
def multiply(x):
return x * n
return multiply
double = multiply_by(2)
print(double(10))Output
20Built-in Higher Order Functions
Python provides several built-in higher-order functions.
Common examples:
- map()
- filter()
- reduce()
- sorted()
- any()
- all()
map() Function
The map() function applies a function to every item in an iterable.
Example
numbers = [1, 2, 3, 4]
result = map(
lambda x: x * 2,
numbers
)
print(list(result))Output
[2, 4, 6, 8]Without map()
numbers = [1, 2, 3, 4]
result = []
for n in numbers:
result.append(n * 2)
print(result)filter() Function
The filter() function selects elements that satisfy a condition.
numbers = [1, 2, 3, 4, 5, 6]
result = filter(
lambda x: x % 2 == 0,
numbers
)
print(list(result))Output
[2, 4, 6]reduce() Function
The reduce() function combines elements into a single value.
from functools import reduce
numbers = [1, 2, 3, 4]
result = reduce(
lambda x, y: x + y,
numbers
)
print(result)Output
10How reduce() Works
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10sorted() as a Higher Order Function
The sorted() function accepts a key function.
students = [
("John", 25),
("Alice", 20),
("Bob", 30)
]
result = sorted(
students,
key=lambda x: x[1]
)
print(result)Output
[('Alice', 20), ('John', 25), ('Bob', 30)]Higher Order Functions with Lambda
Lambda functions are often used with higher-order functions.
square = lambda x: x * x
print(square(5))Output
25Combining map() and Lambda
numbers = [1, 2, 3]
result = map(
lambda x: x ** 2,
numbers
)
print(list(result))Output
[1, 4, 9]Combining filter() and Lambda
numbers = [10, 15, 20, 25]
result = filter(
lambda x: x > 15,
numbers
)
print(list(result))Output
[20, 25]Using Higher Order Functions for Validation
def validate(func, value):
return func(value)
result = validate(
lambda x: x > 0,
10
)
print(result)Output
TrueDecorators Are Higher Order Functions
Decorators are built using higher-order functions.
def decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapperReal-World Applications
Higher-order functions are used in:
- Data processing
- Functional programming
- Web frameworks
- Event handling
- Machine learning pipelines
- API transformations
Advantages of Higher Order Functions
- Cleaner code
- Less repetition
- Improved flexibility
- Better abstraction
- Easier maintenance
Higher Order Functions vs Regular Functions
| Feature | Regular Function | Higher Order Function |
|---|---|---|
| Accept Functions | No | Yes |
| Return Functions | No | Yes |
| Functional Programming | Limited | Strong |
| Reusability | Medium | High |
Best Practices
- Use meaningful function names
- Prefer readability over complexity
- Use lambda functions carefully
- Avoid deeply nested higher-order calls
- Keep functions focused on one task
Common Mistakes
Forgetting to Convert map() Output
Wrong:
result = map(lambda x: x * 2, numbers)
print(result)Output:
<map object>Correct:
print(list(result))Overusing Lambda Functions
Avoid long lambda expressions.
Bad:
lambda x: x*2 if x>5 else x+10 if x<3 else xPrefer normal functions for complex logic.
Summary
Higher Order Functions are functions that accept other functions as arguments or return functions as results. Python's built-in functions such as map(), filter(), reduce(), and sorted() are powerful examples of higher-order functions that simplify coding and support functional programming.
Conclusion
Higher Order Functions are a fundamental feature of Python that enable flexible and reusable code. By mastering concepts such as function passing, returning functions, and using map(), filter(), and reduce(), developers can write more elegant and efficient programs.


0 Comments