Is it possible monkey patch like this?

Marc Aymerich glicerinu at gmail.com
Tue Dec 18 11:26:42 CET 2012


Dear all,
I want to monkey patch a method that has lots of code so I want to avoid copying all the original method for changing just two lines. The thing is that I don't know how to do this kind of monkey patching.

Consider the following code:

class OringinalClass(object):
    def origina_method(self, *args, **kwargs):
        ...
        if some_condition(): # This condition should be changed
            raise SomeException
        ...
        if some_condition():
            ...
            #if some_condition(local_variable): # This condition should be added
            #    raise SomeException 
        ...


Is it possible to tell Python to run the original method without stopping when an exception is raised? so I can catch them on a wrapper method and apply my conditional there.

Any other idea on how to monkey patch those two conditionals ?


Thanks!



More information about the Python-list mailing list