Monkey Patching Magic: Enhancing Functionality in Python

Aman Kumar
3 min readJun 23, 2023

--

Monkey patching is a powerful technique in Python that allows developers to modify or extend the behavior of libraries, modules, classes, or methods at runtime. The term ‘monkey patch’ itself is believed to be derived from ‘guerrilla patch’ — a stealthy, unauthorized intrusion into an established system, similar to a guerrilla soldier creeping through the jungle, except, in this case, it’s a curious monkey navigating through lines of code.

While monkey patching is a powerful tool, it should be used with caution. Modifying the behavior of libraries and classes can introduce hard-to-track bugs and make your code more difficult to understand for other developers. Therefore, it is generally considered a last resort when other options, such as subclassing or wrapping, are not feasible.

Let’s dive in to understand this concept with an example.

Consider a function named ChatCompletion.create provided by the OpenAI package. Let's assume this function takes a prompt as input and generates a completion.

def create(prompt):
# This function sends a prompt to GPT-4 and gets a completion
return completion

Suppose you want to add functionality to this method — say, to log every completion generated. Instead of modifying the original function, which might break other parts of the application that rely on it, you can use monkey patching to extend the function’s behavior.

Here’s how you can do it:

import openai

original_create = openai.ChatCompletion.create

def create_with_logging(prompt):
result = original_create(prompt)
print(f"Generated completion for prompt '{prompt}': {result}")
return result
openai.ChatCompletion.create = create_with_logging

In the above code, we first save a reference to the original ChatCompletion.create method. We then define a new method, create_with_logging, that logs the generated completion and then returns the result. Finally, we replace the original ChatCompletion.create method with our new create_with_logging method.

Now, whenever ChatCompletion.create is called, our create_with_logging method will be invoked instead. This new method still calls the original ChatCompletion.create method, so the original functionality is preserved.

This example illustrates one potential use case for monkey patching — enhancing existing methods with additional functionality, such as logging or timing. However, monkey patching can also be used to fix bugs in external libraries, modify the behavior of a method for testing purposes, or even to hot-fix live systems.

While powerful, monkey patching should be used sparingly and with caution. It can lead to code that is hard to understand and maintain, particularly for developers who are not familiar with the monkey patching technique. Additionally, because monkey patching involves changing behaviors at runtime, it can lead to subtle, hard-to-track bugs. Therefore, it’s often considered best practice to use monkey patching only as a last resort, when other techniques such as subclassing or composition are not feasible.

In conclusion, Python’s dynamic nature, embodied by techniques like monkey patching, is one of the language’s most powerful — and potentially dangerous — features. With great power comes great responsibility. So, next time you find yourself reaching for the monkey patch, make sure you’ve considered all the implications and alternatives. Happy coding!

Let’s connect: Twitter, LinkedIn.

--

--

No responses yet