Monkeypatching in python

A MonkeyPatch is a piece of Python code which extends or modifies other code at runtime (typically at startup). MonkeyPatching would be the practice of writing, or running, a monkeypatch.

A simple example looks like this:

from SomeOtherProduct.SomeModule import SomeClass
def speak(self):
    return "ook ook eee eee eee!"

SomeClass.speak = speak

In this example, if SomeClass did not already have a speak() method, it does now :-) If it had a speak() method before, the new code has replaced the old method definition.

Sources:

Comments

Comments powered by Disqus