detecting property modification

Mangabasi mangabasi at gmail.com
Fri Dec 21 17:39:22 EST 2007


On Dec 21, 4:46 pm, Bruno Desthuilliers
<bdesth.quelquech... at free.quelquepart.fr> wrote:
> Mangabasi a écrit :
> (snip)
>
>
>
> > 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?
>
> Wrong. That means that attribute lookup rules are invoked, which may *or
> not* end up calling the Body *intance*'s __dict__ __setitem__ method to
> be called. Anyway:
>
> >  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?
>
> This still won't solve your problem. What you want to trace are method
> calls on the objects returned by attribute lookup on instances of Body -
> not calls to instances __dict__ methods. In your example, what you want
> to trace are calls to pos.__getitem__, not calls to b.__dict__.__getitem__.
>
> And if you what you really want is to trace any mutator call to any
> arbitrary object that may become an attribute of b, then I'm afraid
> you're in for a hard time...

Now I am curious about these "attribute lookup rules".
My understanding was:

When an attribute name is accessed in an instance

First __getattribute__(self, name) is called.  This guy looks into the
instance's dictionary for the name, if not found then looks into the
parent class's dictionary, if not found searches the base classes, if
not found checks __getattr__, still not found raises an
AttributeError.

Does this look right?  If not, are these attribute lookup rules
documented somewhere?  I have not been paying much attention to the
new style classes, have things changed with the introduction of these
new style classes?




More information about the Python-list mailing list