Fundamentals 4 min read

Higher-Order Functions and Function Composition in Python with 10 Practical Examples

This article explains the core concepts of higher-order functions and function composition in functional programming, defines them, and provides ten practical Python code examples illustrating usage as parameters, return values, composition, and common utilities such as map, filter, reduce, list comprehensions, and functools.partial.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Higher-Order Functions and Function Composition in Python with 10 Practical Examples

Higher-order functions and function composition are core concepts of functional programming that allow functions to be passed as arguments, returned as results, and combined to build more complex behavior.

Definition: A higher-order function is a function that takes one or more functions as parameters and/or returns a function.

Function as parameter example:

def apply_func(func, x):
    return func(x)

def square(x):
    return x ** 2

result = apply_func(square, 5)
print(result)  # 输出: 25

Function as return value example:

def create_adder(n):
    def adder(x):
        return x + n
    return adder

add_5 = create_adder(5)
result = add_5(3)
print(result)  # 输出: 8

Function composition example:

def add(x, y):
    return x + y

def square(x):
    return x ** 2

composed_func = lambda x: square(add(x, 5))
result = composed_func(3)
print(result)  # 输出: 64

Using map() for function mapping:

numbers = [1, 2, 3, 4, 5]

squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers)  # 输出: [1, 4, 9, 16, 25]

Using filter() for conditional filtering:

numbers = [1, 2, 3, 4, 5]

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # 输出: [2, 4]

Using reduce() for accumulation:

from functools import reduce

numbers = [1, 2, 3, 4, 5]

sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers)  # 输出: 15

Using list comprehensions for transformation and filtering:

numbers = [1, 2, 3, 4, 5]

squared_even_numbers = [x ** 2 for x in numbers if x % 2 == 0]
print(squared_even_numbers)  # 输出: [4, 16]

Using functools.partial() for function currying:

from functools import partial

def add(x, y):
    return x + y

add_5 = partial(add, 5)
result = add_5(3)
print(result)  # 输出: 8

These examples demonstrate common applications of higher-order functions and composition, helping you adopt a functional programming style in Python.

PythonFunctional Programmingmapfunction compositionreduceFilterHigher-order Functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.