[Python-3000] Give power to __getattr__

Kay Schluehr kayschluehr at gmx.de
Wed Apr 26 08:27:25 CEST 2006


Nick Coghlan wrote:

> Kay Schluehr wrote:
>
>> While observing heated discussions about adaption and generic 
>> functions I wonder if this
>> is a good opportunity for a modest complaint about the castration of 
>> __getattr__ in new style classes.
>
>
> If you want to get full control back, that's what __getattribute__ is 
> for. . .
>
> Or are you asking for better access to the bits and pieces of 
> object.__getattribute__ so that it's easier to customise the operation?
>
> Cheers,
> Nick.
>
I have likely overestimated the usefullness of __getattr__ for old syle 
classes in case of calling an operator. I have wrongly asserted that 
having an instance x of an old style class X and an implementation of 
__getattr__ on X would pass e.g. '__add__' on a call

 >>> x + 7

to __getattr__. Unlike __getattribute__ for new style classes the 
__getattr__ method actually responds, but it is '__coerce__' that is 
passed instead of '__add__' and that's not much usefull information. 
What I'm looking for is capturing the call of an arbitrary special 
method without implementing it explicitely. This would enable delegating 
a call on __add__ to another class that implements __add__. This could 
either be done by empowering __getattribute__ or use the now residual 
__getattr__ that has no value in case of an __getattribute__ 
implementation. Of course if we are free to choose a name it would be 
better to use a non-confusing name e.g.  __getspecialattr__ that is used 
when the attribute is the name of a special method and the method 
accordingly is neither an instance attribute nor could it be found in 
the class tree.

Ciao,
Kay




More information about the Python-3000 mailing list