Python High Order Function

A higher-order function in Python is a function that either:

  1. Takes one or more functions as arguments.
  2. 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

  1. 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.
  2. The actual uses comprise applying functions on data lists such as map and filter or combining functions to add additional functionalities as in reduce().

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

  1. 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

  1. Code Reusability: Abstracts functionality, so the code is modular.
  2. Simplified Syntax: Reduces boilerplate code.
  3. Improved Readability: Expresses complex logic succinctly.

Practical Use Cases

  1. 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

  1. Higher-order functions are part of the feature set for Python and allow functional programming.
  2. Built-in functions like map, filter, and reduce are some examples.
  3. They allow passing behavior (functions) as arguments or returning behavior from other functions.