I\'m trying to use Python\'s @property decorator on a dict in a class. The idea is that I want a certain value (call it \'message\') to be cleared after it is a
I may be missing what you are trying to do here, but does this solve your problem?
class A(object):
def __init__(self):
self._b = {'message':'',
'last_message': ''}
@property
def b(self):
b = self._b.copy()
self._b['message'] = ''
return b
@b.setter
def b(self, value):
self._b['message'] = value
self._b['last_message'] = value
if __name__ == "__main__":
a = A()
a.b = "hello"
print a.b
print a.b
print a.b["last_message"]
$ python dictPropTest.py
{'last_message': 'hello', 'message': 'hello'}
{'last_message': 'hello', 'message': ''}
hello