Creating an object that can track when its attributes are modified

Chris Angelico rosuav at gmail.com
Wed Mar 6 11:22:56 EST 2013


On Thu, Mar 7, 2013 at 3:07 AM, Ben Sizer <kylotan at gmail.com> wrote:
> I also believe that this won't catch modification to existing attributes as opposed to assignments: eg. if one of the attributes is a list and I append to it, this system won't notice. Is that something I can rectify easily?

The only way you could detect mutation of one of its attributes is
with that object's assistance. Effectively, you would need to have a
subclass of list/dict/tuple/whatever that can respond to the change.
Alternatively, you could retain a deep copy and do a comparison at
time of rollback; this, however, would have annoying consequences wrt
performance and other references and such.

What's the goal of this class? Can you achieve the same thing by
using, perhaps, a before-and-after snapshot of a JSON-encoded form of
the object?

ChrisA



More information about the Python-list mailing list