Python Interview Questions
What is monkey patching in Python?
Monkey patching is the term used to denote the modifications that are done to a class or a module during the runtime. This can only be done as Python supports changes in the behavior of the program while being executed.
 
The following is an example, denoting monkey patching in Python:
# monkeyy.py 
class X: 
     def func(self): 
          print "func() is being called"
The above module (monkeyy) is used to change the behavior of a function at the runtime as shown below:
import monkeyy 
def monkey_f(self): 
     print "monkey_f() is being called"
# replacing address of “func” with “monkey_f
monkeyy.X.func = monkey_f 
obj = monk.X() 
# calling function “func” whose address got replaced
# with function “monkey_f()
obj.func()