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

Paul Prescod paul@prescod.net
Fri, 21 Jul 2000 15:38:25 -0500

Gordon McMillan wrote:
> ...
> You're doing some obsfucating here. Overriding a method with
> multiple base class implementations of that method with the
> desire to chain to one or more of the base class
> implementations of that method is a mess. It's not more of a
> mess because the method is named __getattr__. 
> ...

Solving the particular problem I am trying to solve is extremely messy
in a deep hiearchy with one or more __getattr__ implementations. You
must know about all of the parent getattrs and understand their
semantics enough to to decide what order to call them in. If you think
of getattr as "just another method" then there is no problem but it
isn't just another method. It's a method that is used to implement a
variety of radically different patterns.

> 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.

> 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.

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.

 Paul Prescod - Not encumbered by corporate consensus
New from Computer Associates: "Software that can 'think', sold by
marketers who choose not to."