__getattr__ vs __getattribute__ (was Re: Is "a is b" &c)

Aahz aahz at pythoncraft.com
Mon Mar 17 10:57:55 EST 2003


In article <7Lfda.68908$pG1.1595337 at news1.tin.it>,
Alex Martelli  <aleax at aleax.it> wrote:
>Aahz wrote:
>> In article <Xp1da.94293$zo2.2505671 at news2.tin.it>,
>> Alex Martelli  <aleax at aleax.it> wrote:
>   ...
>>>class X(object):
>>>    def __getattr__(self, name):
>>>        if name == 'f': return []
>>>        raise AttributeError, name
>   ...
>>>NOW, after a=X() or a=Y(), you can choose to:
>>>
>>>    assert a.f is not a.f
>> 
>> s/__getattr__/__getattribute__/
>
>No need!  The code above will work (perhaps a bit more slowly!) with
>the substitution you suggest, but it will work just as well exactly as
>I had posted it.

If you defined __setattr__, I'd agree with you, but with the current
code, as soon as someone does

    a.f = 'foo'

your code is hosed.  I probably should have expanded my comment, but I
didn't think I needed to with you.  ;-)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html




More information about the Python-list mailing list