Monkey Patching in Python

Total Post:113

Points:791
Python 
 268  View(s)
Ratings:
Rate this:

What is Monkey patching in Python ? give a example .

  1. Post:140

    Points:984
    Re: Monkey Patching in Python

    Monkey patching :

    A dynamic modification of class module patch at run time in python this called monkey patching term. 

    example:

    # m.py 
    class MyClass:
    def f(mindstick):
    print "f()"

    We can then run the monkey-patch testing like this:

    import m 
    def monkey_f(mindstick):
    print "monkey_f()"

    m.MyClass.f = monkey_f
    obj = m.MyClass()
    obj.f()

    The output will be as below:

    monkey_f() 

    in above example some changes in the behavior of f() in My Class using the function. monkey_f(), outside of the module m. 

Answer