Inefficiency of __getattr__

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Oct 1 05:59:00 EDT 2000


hzhu at yahoo.com (Huaiyu Zhu) writes:

> Is __getattr__ supposed to be run only when the attribute is not already
> defined?  

Yes, that is the case.

> This does not seem to be so and it is very costly (equivalent of
> addition of two 1000 element vectors).  In the following example __getattr__
> should not be invoked even if it is there:
[...]
> a = A()
> def test():
>     for i in xrange(10000):     a+a

Computing a+a will first invoke __coerce__. Since that is not defined,
it will invoke __getattr__ - this is what happens. Defining __coerce__
would fix this problem.

Regards,
Martin




More information about the Python-list mailing list