Python 2.5 (r25, Oct 30 2006, 20:50:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
.... def __float__(self): return 10.0>>from cmath import sqrt
class NumericType1(object):
....
.... def __complex__(self): return 10+0j>>class NumericType2(object):
....
(3.16227766017+0j)>>print sqrt(NumericType1())
Traceback (most recent call last):>>print sqrt(NumericType2())
File "<stdin>", line 1, in <module>
TypeError: a float is required
Having recently made the pleasant discovery that math.sqrt and friends
will happily accept any object with a working __float__ attribute, I
expected the same to be true with regards to cmath and __complex__.
The bit that I find really counter-intuitive is that the cmath
functions will use __float__ if available, but completely ignore
__complex__.
Does anyone know of a good reason for the above behaviour? Would a
patch to complexobject.c that `fixes' this be of any interest to
anyone but me? Or would it likely break something else?
Mark