I do not agree that the selected answer is an ideal way to override property methods. If you expect getters and setters to be overridden, then you can use a lambda to provide access to yourself with something like lambda self: self.<property func> .
This works (at least) for Python versions 2.4 to 3.6.
If anyone knows a way to do this, using the property as a decorator, rather than directly calling the property (), I'd love to hear that!
Example:
class Foo(object): def _get_meow(self): return self._meow + ' from a Foo' def _set_meow(self, value): self._meow = value meow = property(fget=lambda self: self._get_meow(), fset=lambda self, value: self._set_meow(value))
Thus, overriding can be easily done:
class Bar(Foo): def _get_meow(self): return super(Bar, self)._get_meow() + ', altered by a Bar'
so that:
>>> foo = Foo() >>> bar = Bar() >>> foo.meow, bar.meow = "meow", "meow" >>> foo.meow "meow from a Foo" >>> bar.meow "meow from a Foo, altered by a Bar"
I found this on a geek in a game .
Mr. B Jan 16 '13 at 0:55 2013-01-16 00:55
source share