Monkey Patching in Python (Dynamic Behavior)
In Python, the term monkey patch refers to dynamic (or run-time) modifications of a class or module. In Python, we can actually change the behavior of code at run-time.
# monk.pyclass A: def func(self): print ("func() is being called") |
We use above module (monk) in below code and change behavior of func() at run-time by assigning different value.
import monkdef monkey_f(self): print ("monkey_f() is being called") # replacing address of "func" with "monkey_f"monk.A.func = monkey_fobj = monk.A() # calling function "func" whose address got replaced# with function "monkey_f()"obj.func() |
Examples:
Output :monkey_f() is being called
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


