Nuno Bispo
1 supporter
Understanding Monkey Patching in Python

Understanding Monkey Patching in Python

Nov 28, 2023

Monkey patching, a notable yet contentious technique in Python, involves dynamically modifying classes or modules at runtime to alter or enhance their behavior.

This method enables developers to inject new code into existing codebases to address bugs, add features, or tailor functionalities without altering the original source code.

Particularly effective in Python due to its dynamic nature, monkey patching is often used to adjust standard library functions, modify third-party modules, or customize system internals for specific projects, especially in complex codebases where direct source modification is impractical.

Despite its utility, monkey patching is a subject of debate within the Python community, largely due to concerns about code maintainability, readability, and the introduction of hard-to-trace bugs.

These modifications can render codebases fragile and overly reliant on the internal mechanics of altered modules, which might evolve over time.

Discussions about best practices for monkey patching are ongoing, with some advocating for its pragmatic use in solving complex issues, while others caution against its potential to create more problems than it solves.

The Basics of Monkey Patching

Monkey patching in Python refers to the dynamic (or runtime) modification of a class or module, allowing developers to alter or extend their functionalities without changing the original source code.

How it Works

Identify the target: Determine the class, method, or function you want to modify.

Define the new /strong> Create a new function or method that has the desired behavior.

Replace the original: Assign your new function or method to the class or module you're modifying.

Example of Monkey Patching in Action

Consider a scenario where you're using a third-party library with a function calculate_sum that sums up two numbers.

Suppose you want to modify this function to always add an extra 1 to the result.

Original function in the library:

def calculate_sum(a, b):
    return a + b

Monkey patching to modify calculate_sum:

import some_library

# Original function
original_calculate_sum = some_library.calculate_sum

# New behavior
def new_calculate_sum(a, b):
    return original_calculate_sum(a, b) + 1

# Replacing the original function
some_library.calculate_sum = new_calculate_sum

Now, whenever calculate_sum is called, it will use your patched version, adding 1 to the result.

Full article at:

Enjoy this post?

Buy Nuno Bispo a coffee

More from Nuno Bispo