Understanding Monkey Patching in Python: A Beginner's Guide

Posted by Afsal on 30-May-2025
Hi Pythonistas!
Today we are discussing an advanced technique in python which is called monkey patching.
 
What is Monkey Patching?
 
Monkey Patching in Python is the practice of modifying or extending modules or classes at runtime. This can involve adding, changing, or even removing methods and attributes of classes or modules dynamically. While the term monkey patching may sound a bit funny, it refers to a powerful concept that can be used to fix bugs, extend functionality, or modify behavior without changing the original source code. However, it should be used with caution, as it can make code harder to understand and maintain.
 
How Does Monkey Patching Work?
 
Python allows you to modify classes, methods, and functions at runtime. This is typically done by assigning new methods or attributes to an existing class or module. The change is immediately reflected wherever that class or module is used, without the need to modify the original codebase.
 
A Simple Example of Monkey Patching
 
Let’s say you have a class that has a method, and you want to change its behavior without modifying the original class. Here’s an example:
In [2]: class MyClass:
...: def greet(self):
...: return "Hello"
...:
In [3]: obj = MyClass()

In [4]: obj.greet()
Out[4]: 'Hello'

In [5]: def new_greet(self):
...: return "Hii"
...:

In [6]: MyClass.greet = new_greet

In [7]: obj.greet()
Out[7]: 'Hii'

In [8]:
We first define a class MyClass with a greet method.
We create an object obj of MyClass and call the greet method, which outputs "Hello!".
Then, we define a new function new_greet that changes the greeting to "Hi there!".
Finally, we assign the new method new_greet to the greet method of MyClass using MyClass.greet = new_greet.
After patching, the method greet now returns "Hi there!" instead of "Hello!".
 
Real-World Use Case
 
One of the most common use cases for monkey patching is when you are working with third-party libraries that you cannot modify. Suppose you want to fix a bug in a library or add new functionality without changing its code directly.
Here’s a realworld example where we patch a library's method:
In [1]: import requests

In [2]: original_get = requests.get

In [3]: def patched_get(url):
...: response = original_get(url)
...: if response.status_code == 404:
...: return "Page not found!"
...: return response.text
...:
In [4]: requests.get = patched_get

In [5]: requests.get('http://example.com/test')
Out[5]: 'Page not found!'
In this case:
We patch the requests.get method to handle 404 errors differently by returning a custom message.
We save the original get method and later restore it if necessary.
 
Advantages of Monkey Patching
 
Fix Bugs Dynamically: If a bug is found in a third-party library, you can patch it without waiting for the maintainers to fix it.
Extend Functionality: You can add new features or modify existing ones in a library that you can't change directly.
Testing and Prototyping: In testing or prototyping, you can quickly mock or change methods of existing objects.
 
Risks of Monkey Patching
 
Maintenance Problems: Over time, it can become difficult to track where changes were made, especially if multiple patches are applied across a large codebase.
Compatibility Issues: If the library or module you’re patching is updated, your patch might break or cause unexpected behavior.
Confusion: Other developers who are unfamiliar with your code might be confused by the unexpected changes, making collaboration harder.
 
Best Practices
 
Use with caution: Monkey patching is powerful, but it should be used sparingly. It's best for quick fixes and specific use cases.
Document your changes: If you do apply monkey patches, make sure they are well-documented so others can understand why and how they were applied.
Test thoroughly: Always test the patched code to ensure the changes don’t cause unintended side effects.
 
When to Avoid Monkey Patching
 
When modifying core libraries: If you need to modify a widely used core library, it might be better to contribute a patch back to the project instead of patching it locally.
When performance is critical: Monkey patching adds an additional layer of complexity and can affect the performance, especially if done extensively.
 
Monkey patching can be a useful tool in certain situations, but it should be approached with care. While it allows for dynamic changes to code at runtime, it can lead to maintenance challenges and bugs if overused or applied incorrectly. Always ensure that changes made via monkey patching are well-documented, thoroughly tested, and used only when necessary.By understanding and using monkey patching wisely, you can make your Python applications more flexible and powerful without needing to modify every piece of code.
 
Hope you have learned a new concept from this post please share your valuable suggestions with afsal@parseltongue.co.in