Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a decorator to an existing object method?

If I'm using a module/class I have no control over, how would I decorate one of the methods?

I understand I can: my_decorate_method(target_method)() but I'm looking to have this happen wherever target_method is called without having to do a search/replace.

Is it even possible?

like image 679
Phillip B Oldham Avatar asked Sep 14 '09 09:09

Phillip B Oldham


Video Answer


2 Answers

Don't do this.

Use inheritance.

import some_module

class MyVersionOfAClass( some_module.AClass ):
    def someMethod( self, *args, **kwargs ):
        # do your "decoration" here.
        super( MyVersionOfAClass, self ). someMethod( *args, **kwargs )
        # you can also do "decoration" here.

Now, fix you main program to use MyVersionOfAClass instead of some_module.AClass.

like image 130
S.Lott Avatar answered Oct 30 '22 11:10

S.Lott


Yes it's possible, but there are several problems. First whet you get method from class in obvious way you get a warper object but not the function itself.

class X(object):
    def m(self,x):
        print x

print X.m           #>>> <unbound method X.m>
print vars(X)['m']  #>>> <function m at 0x9e17e64>

def increase_decorator(function):
    return lambda self,x: function(self,x+1)

Second I don't know if settings new method will always work:

x = X()
x.m(1)         #>>> 1
X.m = increase_decorator( vars(X)['m'] )
x.m(1)         #>>> 2
like image 44
Arpegius Avatar answered Oct 30 '22 09:10

Arpegius