I'm not quite sure how to use a decorator on an inherited method. Normally decorators are put before the definition but for an inherited function the definition is given in the parent and not the child class. I would like to avoid rewriting the definition of the method in the child class and simply specify to put the decorator around the inherited method.
To make myself clearer, here is a working example of what I mean:
class Person():
def __init__(self, age):
self.age = age
@classmethod
def gets_drink(cls, ):
print "it's time to get a drink!"
return "A beer"
def dec(some_f):
def legal_issue(obj,):
some_f(obj)
return 'A Coke'
return legal_issue
class Child(Person):
def __init__(self, age):
Person.__init__(self, age=age)
#in principle i'd like to write
#@dec
#self.gets_drink
#which does not work
#the only working way I found is by defining the method as a classmethod
#and write this:
@dec
def gets_drink(self, ):
return Person.gets_drink()
dad = Person(42)
son = Child(12)
print dad.gets_drink()
print son.gets_drink()
The way it is done in this example works, but it looks somehow weird to me, replacing the method with a call to the original method only to be able to put the decorator. Also, for this to work, the method needs to be defined as a classmethod in the parent class.
Am I missing something? How would experienced developers do this?
Any help, insights, critical comments and free coffees are much appreciated!