Inefficiency of __getattr__
Huaiyu Zhu
hzhu at yahoo.com
Mon Oct 2 04:05:43 EDT 2000
On 01 Oct 2000 11:59:00 +0200, Martin von Loewis
<loewis at informatik.hu-berlin.de> wrote:
>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.
>
Thanks for this info. It seems that
def __coerce__(self, other): return self, other
works and cut the overhead by at least half.
However, I'm not sure how is __coerce__ supposed to work. The documentation
has just a very terse description:
coerce (x, y)
Return a tuple consisting of the two numeric arguments converted to a
common type, using the same rules as used by arithmetic operations.
Does this mean
a+b == apply(a.__class__.__add__, a.__coerce__(b))
None of my three books mention coerce.
Huaiyu
More information about the Python-list
mailing list