optimization with the "compiler" module

Steve Holden sholden at holdenweb.com
Wed Apr 17 09:23:31 EDT 2002


"Philip Swartzleonard" <starx at pacbell.net> wrote in message
news:Xns91F32355A8FA9RASXnewsDFE1 at 130.133.1.4...
> Andrew Dalke || Tue 16 Apr 2002 04:35:54p:
>
[ Andrew Dalke defines a Strange() object ]
>
> In this case, wouldn't 3+7.0+x be a Typeerror or something? Int doesn't
> know how to add with a Strange, and Strange dosen't have an upgrading
> function... or do we assume addition is always commutitive and try '3+x+
> 7.0' (effectively), therefore breaking the optimization mentioned by the
> other... =)
>
In Python it takes very little time to check whether your assertion is true.
Readers are encouraged to perform the following experiment ;-) ...

>>> class Strange:
...  def __add__(self, other):
...   return self
...
>>> x = Strange()
>>> x+3+7.0
<__main__.Strange instance at 0x014CE610>

The point of Andrew's example is precisely that Strange() instances can have
values of *any* type added to them, since their __add__() method does not
ever refer to the second operand. So there's absolutely no reason why any
TypeError [or something] exception should be raised.

When you refer to an "upgrading function" I suspect you mean a coercion?
There's no need for a coercion, since for any Strange instance s, s+x == s
for any x (and, indeed, s+x is x as well)\

regards
 Steve







More information about the Python-list mailing list