Is there a preferred way of decorating a class property?
As I see it you could either decorate the property itself with another property or you could decorate the underlying method and then apply @property
on that.
Are there any considerations to make for either approach?
def decorate_property(prop):
@property
def inner(instance):
return prop.__get__(instance) + 1
return inner
def decorate_func(func):
def inner(instance):
return func(instance) +1
return inner
class A:
x = 1
@decorate_property
@property
def f(self):
return self.x
@property
@decorate_func
def g(self):
return self.x
a = A()
print(a.f) # 2
print(a.g) # 2