What is monkey patching?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Monkey patching is a technique in dynamic languages where you modify or extend classes, modules, or functions at runtime without changing their source code. In Python, this means replacing methods, adding attributes, or overriding behavior on existing objects after they are defined. While monkey patching enables quick fixes, testing mocks, and third-party library extensions, it can make code harder to debug and maintain because the modifications are invisible at the source level.
How Monkey Patching Works
In Python, classes and modules are mutable objects. You can assign new functions to their attributes at any time:
The patch modifies the class itself, so all instances — even ones created before the patch — see the new behavior.
Adding New Methods
Patching Module-Level Functions
Patching Instance Methods
Patch a single instance without affecting the class or other instances:
types.MethodType binds the function to the specific instance so self is passed correctly.
Use Case: Testing with unittest.mock
The most accepted use of monkey patching is in testing, where unittest.mock.patch temporarily replaces objects:
@patch monkey patches requests.get for the duration of the test and automatically restores it afterward.
Using patch as a Context Manager
Use Case: Extending Third-Party Libraries
Monkey Patching in Other Languages
Ruby
JavaScript
Risks and Drawbacks
Common Pitfalls
- Patching the wrong import path: In Python,
patch('module_a.requests.get')must match whererequestsis imported, not where it is defined. Ifmodule_adoesfrom requests import get, patchmodule_a.get, notrequests.get. - Forgetting to restore the original: Without
unittest.mock.patchor atry/finallyblock, a monkey patch persists for the entire process lifetime. Always restore the original function after patching, especially in tests. - Patching built-in types: Patching built-in types like
str,int, orlistis not allowed in CPython (they are implemented in C). Use wrapper classes or functions instead. - Breaking other tests with global patches: A monkey patch applied in one test leaks into subsequent tests if not cleaned up. Use
unittest.mock.patchas a decorator or context manager to ensure automatic cleanup. - Making code untestable by relying on monkey patches in production: If your production code depends on monkey patches to function, it becomes fragile and hard to reason about. Use proper dependency injection, subclassing, or composition instead.
Summary
- Monkey patching modifies classes, modules, or functions at runtime without changing source code
- In Python, assign new functions to class attributes:
MyClass.method = new_function - The primary legitimate use is in testing with
unittest.mock.patch, which patches temporarily - Avoid monkey patching in production code — prefer dependency injection, subclassing, or adapter patterns
- Always restore original behavior after patching, either manually or with context managers

