general class functions

Andrea Griffini agriff at tin.it
Wed Nov 3 21:36:13 CET 2004


On 3 Nov 2004 08:22:24 -0800, syd.diamond at gmail.com (syd) wrote:

>Andrea Griffini <agriff at tin.it> wrote in message
>
>> Would it be worse or better to make Library a new-style
>> class, using __getattribute__ instead of __getattr__ and
>> defaulting to object.__getattribute__(self,name) in the
>> "else" part ?
>
>Can you elaborate here, please?  I'm not sure I understand what you mean.

IIUC In python there are two ways of "intercepting"
when someone tries to access a member of an instance
of a class. The first way is defining "__getattr__"
(a method that is called when the normal lookup in
the instance __dict__ failed) and another is defining
"__getattribute__" that is called BEFORE trying any
other lookup (the latter works only for new-style
classes; those that inherit from 'object').

In the first case you need to raise AttributeError
for members you don't want to "emulate" to have the
interpreter continue the lookup; in the second case
you've instead to chain the request up in the
class hierarchy (e.g. to 'object').

Probably as often happens there are pros and cons
with each solution; but I'm quite new to python and
basically virgin of any python internal trick... so
I was asking in the NG for an explanation.

Looks like you're a newbie too; I wouldn't worry ...
probably there are a *lot* of python programmers out
there that never investigated on those implementation
details and still can live happy :-)

Andrea



More information about the Python-list mailing list