You could implement this using a descriptor, i.e. as follows:
class MyProperty(object):
def __init__(self, name):
self.name = name
def __get__(self, instance, owner):
if instance is None:
return self
else:
# get attribute from the instance
return getattr(instance, '_%s' % self.name) # return x._prop
def __set__(self, instance, value):
# set attribute and the corresponding key in the "remote" dict
instance.other_dict[self.name] = value # x.other_dict["prop"] = value
setattr(instance, '_%s' % self.name, value) # x._prop = value
And use them as follows:
class MyClass(object):
prop = MyProperty("prop")
another_prop = MyProperty("another_prop")
As a side note: it might be worth thinking about whether you really need to duplicate the properties values. You could easily get rid of the _prop
attribute completely by returning the corresponding value from other_dict
. This also avoids potential issues arising from different values stored in the dict and on your class instance - which may easily occur with your current scheme.