Python High Order Function
A higher-order function in Python is a function that either:
- Takes one or more functions as arguments.
- Returns a function as its result.
This brings much abstraction and flexibility in using functional programming techniques with Python.
Main Features of Higher-Order Functions
- First-Class Functions: In Python, functions are said to be first-class functions; they can not only be assigned to variables but also passed as arguments to other functions and returned from functions.
- The actual uses comprise applying functions on data lists such as
map
andfilter
or combining functions to add additional functionalities as inreduce()
.
Examples of Higher-Order Functions
1. Functions as Arguments
Example with map
:
# Function to double a number
def double(x):
return x * 2
# Using map (higher-order function)
numbers = [1, 2, 3, 4]
doubled = map(double, numbers) # Pass `double` as an argument
print(list(doubled)) # Output: [2, 4, 6, 8]
Here, map
is a higher-order function because it takes another function (double
) as an argument.
2. Functions Returning Functions
Example of a function returning another function:
def multiplier(factor):
def multiply(x):
return x * factor
return multiply
# Using the function
double = multiplier(2) # Creates a function that doubles
triple = multiplier(3) # Creates a function that triples
print(double(5)) # Output: 10
print(triple(5)) # Output: 15
Here, multiplier
is a higher-order function because it returns a function.
3. Built-in Higher-Order Functions
map
: Applies a function to each item in an iterable.
numbers = [1, 2, 3, 4]
squared = map(lambda x: x**2, numbers) # Using a lambda function
print(list(squared)) # Output: [1, 4, 9, 16]
2. filter
: Filters elements of an iterable based on a function.
numbers = [1, 2, 3, 4, 5]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens)) # Output: [2, 4]
3. reduce
(from functools
): Reduces an iterable to a single value by applying a function.
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda x, y: x + y, numbers)
print(total) # Output: 10
Advantages of Higher-Order Functions
- Code Reusability: Abstracts functionality, so the code is modular.
- Simplified Syntax: Reduces boilerplate code.
- Improved Readability: Expresses complex logic succinctly.
Practical Use Cases
- Data Transformation:
data = [1, 2, 3, 4]
transformed = map(lambda x: x * 10, data)
print(list(transformed)) # Output: [10, 20, 30, 40]
2. Filtering Data:
data = [10, 15, 20, 25]
filtered = filter(lambda x: x > 15, data)
print(list(filtered)) # Output: [20, 25]
3. Custom Function Builders:
def power_function(power):
return lambda x: x ** power
square = power_function(2)
cube = power_function(3)
print(square(4)) # Output: 16
print(cube(2)) # Output: 8
Key Points to Remember
- Higher-order functions are part of the feature set for Python and allow functional programming.
- Built-in functions like
map
,filter
, andreduce
are some examples. - They allow passing behavior (functions) as arguments or returning behavior from other functions.