detecting property modification

Jean-Paul Calderone exarkun at divmod.com
Fri Dec 21 13:11:55 EST 2007


On Fri, 21 Dec 2007 09:49:51 -0800 (PST), Mangabasi <mangabasi at gmail.com> wrote:
> [snip]
>
>Hi Jean-Paul,
>
>Sorry, I should have spelled this out in my post but I did not.  For
>several reasons I do not wish to couple the pos object with the Body
>instances.  In my case, I did not want pos objects (in my example they
>were lists but they can be other objects as well) to care about their
>parents.  So the question is:  How do we detect this in the Body?
>

You can't.  If you have this:

    b = Body([1, 2])
    b.pos[0] = 3


Then Body gets asked for the value of the attribute; then, that
object (a list in the case of this example) gets told to change
its 0th element to 3.  There is no way for Body to get in the
way of the second message, except by doing what I said - adding
a wrapper around the list so that it _can_ get in the way.

This doesn't require the list to know anything about Body, though.
_Body_ should apply the wrapper.

For example, new-style:

    class Body(object):
        def get_pos(self):
            return ChangeNoticingListWrapper(self._pos)
        def set_pos(self, value):
            self._pos = value
        pos = property(get_pos, set_pos)
        def __init__(self, pos):
            self.pos = pos

    class ChangeNoticingListWrapper(object):
        def __init__(self, wrapped):
            self.wrapped = wrapped
        def __setitem__(self, index, value):
            print 'Changing', index, 'to', value
            self.wrapped[index] = value

    b = Body([1, 2])
    b.pos[0] = 3

Jean-Paul



More information about the Python-list mailing list