0 votes
in Python by
What do you understand by monkey patching in Python?

1 Answer

0 votes
by

The dynamic modifications made to a class or module at runtime are termed as monkey patching in Python. Consider the following code snippet:

# m.py

class MyClass:

def f(self):

print "f()"

We can monkey-patch the program something like this:

import m

def monkey_f(self):

print "monkey_f()"

m.MyClass.f = monkey_f

obj = m.MyClass()

obj.f()

The output for the program will be monkey_f().

The examples demonstrate changes made in the behavior of f() in MyClass using the function we defined i.e. monkey_f() outside of the module m.

Related questions

0 votes
asked May 16, 2020 in Python by Robindeniel
0 votes
asked Aug 29, 2020 in Python by Robindeniel
...