On Dec 21, 1:11 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Fri, 21 Dec 2007 09:49:51 -0800 (PST), Mangabasi <[EMAIL PROTECTED]> 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


Hi Jean-Paul,

I see what you are saying but my contrived example is not doing a good
job explaining the real issue I am having here.

When you say "The Body gets asked for the value of the attribute" that
means that Body's __dict__ is being asked to provide a value
corresponding to its 'pos' key, right?  Now I may want to ask a more
specific question here.  How do I modify Body's __dict__ so that I can
perform a task when __getitem__ method of the __dict__ is called?
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to