Kay Schluehr wrote: > Robin Becker wrote: > >>Is there a way to override a data property in the instance? Do I need to >>create >>another class with the property changed? >>-- >>Robin Becker > > > It is possible to decorate a method in a way that it seems like > property() respects overridden methods. The decorator cares > polymorphism and accesses the right method. > > def overridable(f): > def __wrap_func(self,*args,**kwd): > func = getattr(self.__class__,f.func_name) > if func.func_name == "__wrap_func": > return f(self,*args,**kwd) > else: > return func(self,*args,**kwd) > return __wrap_func > > > class A(object): > def __init__(self, x): > self._x = x > > @overridable > def get_x(self): > return self._x > > x = property(get_x) > > class B(A): > > def get_x(self): > return self._x**2 > > class C(B):pass > > >>>>a = A(7) >>>>a.x > > 7 > >>>>b = B(7) >>>>b.x > > 49 > >>>>c = C(7) >>>>c.x > > 49 >
I thought that methods were always overridable. In this case the lookup on the class changes the behaviour of the one and only property. -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list