There is sample code as below:
class Cage(object):
def __init__(self, func, parent):
self.func = func
self.parent = parent
def __call__(self):
print('Decorator Cage')
print(self.parent.age) # I need to use instance's object of Dog
print(self.parent.name)
self.func()
class Dog(object):
def __init__(self, age, name):
self.age = age
self.name = name
def dosomedecorate(self):
print('Do something')
@Cage
def dosugar(self):
print('Do something')
if __name__ == "__main__":
poppy = Dog(20, 'Louis')
Cage(poppy.dosomething, poppy)() # It works, but looks weird...
poppy.dosugar() # Use syntactic sugar will raise error
In this case,
I want to decorate function in class Dog with decorator class Cage.
But, Cage need to use object in Dog
I tried to use syntactic sugar as below:
@Cage
def dosugar(self):
print('Do something')
, then raise error:
TypeError: __init__() missing 1 required positional argument: 'parent'
Is it possible to pass self to decorator?
Or other way to deal with this kind of case?
I would do it like that: