Getting a dictionary from an object

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Jul 23 09:03:17 EDT 2005


Thanos Tsouanas a écrit :
> On Sat, Jul 23, 2005 at 12:06:57PM +0200, Paolino wrote:
> 
>>use getattr(self.obj,key) possibly, as __getattribute__ gets total 
>>control on attribute access
> 
> 
> Thanks, but what do you mean by 'total control'?

<mode="screamin jay hawkins">
__getattribute__ is really some kind of an evil black voodoo mumbo jumbo 
juju magic method...
</mode>

More seriously:
__getattr__() (if defined) is called when and if the 'normal' attribute 
lookup process failed. It's main use is to delegate attribute access to 
a proxied/wrapped/decorated object.
__getattribute__() (if defined) completely override the attribute lookup 
process - so it gets full control over attribute access.

This does not directly concern what you're doing now, but still, for a 
number of reasons, the canonical pythonic way to do named attribute 
access is with the builtin getattr() function, that will call on the 
appropriate lookup mechanism.

As a general rule, '__magic__' methods are implementation, not API, so 
you shouldn't call on them directly unless you have a reaaaally goooood 
reason to do so.

HTH
Bruno




More information about the Python-list mailing list