In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, As a beginner its really difficult to me understand this term in context of python. Can anybody explain to me with a real world example How exactly we do ?
- dynamic modifications of a class
- dynamic modifications of module at runtime
I am insisting a real world example (as simple as possible) to understand in which scenarios we have to do such task ?
Monkey-patching is a way to make some global under-the-hood change in a way that existing code will continue to run, but with modified behavior.
A really really trivial example of changing the behavior of the builtin
strcommand:b.py
a.py
The
amodule has modified the behavior of other code using thestrcommand, by patching it to useunicodeinstead. This would be necessary since we pretend that we have no access tob.py‘s code. It could have been a huge package that we simply use and can’t change. But we can slip in new code to be called that changes the behavior.A real world example from gevent