Hot Patching Python Objects

June 4, 2012

Hot patching is a touchy issue, it’s often frowned upon, and rightly so. But sometimes it’s the easiest way to get what you want.

Usually in Python you would patch a class with a new method, which works fine. But what if you only wanted to patch a single instance of that class, and not all of them?

Turns out that’s pretty easy with the types library.

To use it you define your patch method as a regular function, then you call types.MethodType to properly bind it, and you are done!

Here is a simple example:

Easy!

Categories: Geek
Tags: , ,

Leave A Comment

Your email will not be published.