[Tutor] Re: special object method for method calls?
Gonçalo Rodrigues
op73418 at mail.telepac.pt
Wed Nov 12 06:24:50 EST 2003
On Tue, 11 Nov 2003 19:43:03 -0800, you wrote:
>Abel Daniel wrote:
>
>> Jeff Shannon writes:
>>
>>
>>>def __getattr__(self, attr):
>>> if iscallable(self.__dict__[attr]):
>>> # do whatever here
>>> return self.__dict__[attr]
>>
>>
>> This won't work. From http://python.org/doc/current/ref/attribute-access.html:
>>
>> "Note that if the attribute is found through the normal
>> mechanism, __getattr__() is not called."
>>
>> So calling self.__dict__[attr] in __getattr__ will result in a
>> KeyError.
>
>Hm, you're right. That's odd, I'd have *sworn* that __getattr__()
>could be used to intercept lookups of existing attributes. Or at
>least that *something* could be. Unfortunately I don't have time to
>track this down right now...
>
What you want is __getattribute__ which intercepts *every* attribute
lookup. It only works with *new-style* classes though.
With my best regards,
G. Rodrigues
More information about the Tutor
mailing list