Skip to content Skip to sidebar Skip to footer

Monkey-patching Python An Instance Method

I'm trying to monkey-patch a class instance, but don't quite see how I can patch a class method no problem. >>> class Simple(object): ... def make(self, arg): ...

Solution 1:

Methods only get the self arg passed automatically when received from the class (not from the instance dict), so you'll need to pass an 1-arg function there:

s.make = lambda arg: times_eight(s, arg)

(You can simplify that with functools.partial.)

Post a Comment for "Monkey-patching Python An Instance Method"