Unleash the Power of Monkey Patching in Python

Suryan Saravanan
2 min readJul 8, 2023

Welcome to the fascinating world of monkey patching in Python!

Let’s dive in and explore the concept of monkey patching and demonstrate its usage through practical examples

Monkey patching involves modifying or extending an existing codebase without altering the original source code. It allows developers to add, modify, or replace methods, attributes, or functions of objects dynamically. The name “monkey patching” comes from the playful idea of a monkey tinkering with code, adding its own patches to suit its needs.

Fixing a Bug in a Third-Party Library

Imagine you’re working on a project that relies heavily on a third-party library. Unfortunately, you encounter a bug that affects your application’s functionality, and a fix is not available yet. Monkey patching comes to the rescue! Let’s consider a hypothetical scenario where a function calculate in the library has a bug that causes incorrect results. You can fix it temporarily using monkey patching

import third_party_library

def patched_calculate(x, y):
result = third_party_library.calculate(x, y)
# Fix the bug by modifying the result
return result + 5

third_party_library.calculate = patched_calculate

# Now, when you use the calculate function from the library, it incorporates your fix.

Extending the Functionality of an Existing Class

Sometimes, you may need to add extra functionality to an existing class, even if you don’t have control over its implementation. Monkey patching allows you to extend the class with additional methods or attributes. Let’s say we have a User class that lacks a method to display the user's full name. We can monkey patch the class to add the missing method

class User:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name

def get_full_name(self):
return f"{self.first_name} {self.last_name}"

User.get_full_name = get_full_name

# Now, all instances of the User class have the get_full_name method.

Testing and Mocking

Monkey patching is particularly useful in testing scenarios. It allows you to replace dependencies or modify behavior temporarily to facilitate effective testing. Let’s consider a situation where a function relies on an external API that we want to mock for testing purposes

import external_api

def mock_api_response():
return "Mocked API response"

external_api.fetch_data = mock_api_response

# By monkey patching the fetch_data function, we can provide a mocked response during testing.

Conclusion

Monkey patching provides developers with a powerful tool to modify or extend existing code at runtime, opening up endless possibilities for customization and experimentation. While it offers great flexibility, it’s crucial to use monkey patching judiciously, as it can introduce complexity and potential conflicts. By understanding the concept and exploring practical examples, you can leverage monkey patching effectively to solve bugs, enhance existing functionality, and streamline your development process in Python. Embrace the art of monkey patching and unleash your creativity!

--

--