Override property setter of base class in Python 3
Nagy László Zsolt
gandalf at shopzeus.com
Sun Sep 11 06:02:49 EDT 2016
By the way, I know that I can use a "property virtualizer", something
like this:
import inspect
class A:
def __init__(self, prop=0):
self.__prop = prop
def _prop_get(self):
return self.__prop
def _prop_set(self, value):
self.prop_set(value)
def prop_set(self, value):
print("Setting prop value in A to", value)
self.__prop = value
prop = property(_prop_get, _prop_set)
class B(A):
def prop_set(self, value):
print("Setting prop value in B to", value)
super().prop_set(value)
class C(A):
def prop_set(self, value):
print("Setting prop value in C to", value)
super().prop_set(value)
class D(B,C):
pass
d = D(0)
d.prop=10
But this solution almost defeats the purpose of properties. E.g. a
property should look like an attribute, and its behaviour should be
manipulated through its name (and not another special method that must
be exposed to subclasses.)
More information about the Python-list
mailing list