[Python-Dev] Pre-PEP: Attribute Access Handlers v2

Gordon McMillan gmcm@hypernet.com
Fri, 21 Jul 2000 17:27:29 -0400

Paul Prescod wrote:

> Gordon McMillan wrote:

> > This proposal changes those semantics. Hooking an attribute
> > masks anything in a base class with that name. That's probably
> > a good thing, but it should be made explicit since that's not
> > the way __getattr__ behaves.
> I don't see the difference.
> def __attr_XXX__( self, op, va ):
>     Parent.__attr_XXX__( self, op, val )
> You can still chain if you want.

You can? Your proposal only specified that when the class 
object is being created, method names of the form 
__attr_XXX__ would cause stuff to happen in the way the class 
is built. You never said that  accesses to things named 
__YYY_XXX__ on another object would also get transformed 
into something else.

Also, what if the base class just has "attr" - not a hook at all? 
It will be masked by the hook (which is the difference in 
behavior I was pointing out).
> > Similarly, there's a difference from __setattr__. If you assign
> > to the instance.__dict__, the hook will no longer apply for
> > that instance / attribute.
> That's true.
> > Also note that while this is *safer* than __xxxattr__, it's
> > still not *safe*. Infinite recursion is less likely, but still
> > easily achieved.
> The ability to recurse infinitely is a property of methods in
> general, not this method in particular. Okay, it is a little more
> likely because of a logic flaw but I don't see that as being
> unsafe.

And earlier I said:
> > Overriding a method with
> > multiple base class implementations of that method with 
> > desire to chain to one or more of the base class
> > implementations of that method is a mess. It's not more of 
> > mess because the method is named __getattr__. ...

So I guess we're even, since we've used the same argument 
both ways.
> I see the current __getattr__ hacks as being unsafe because in a
> deep inheritance hierarchy a seemingly insignicant addition in
> one place will break code somewhere else.

Deep inheritance hierarchies are unpythonic <0.001 wink>.

- Gordon