I was a little surprised by the following behaviour: 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. >>> from cmath import sqrt >>> class NumericType1(object): ... def __float__(self): return 10.0 ... >>> class NumericType2(object): ... def __complex__(self): return 10+0j ... >>> print sqrt(NumericType1()) (3.16227766017+0j) >>> print sqrt(NumericType2()) Traceback (most recent call last): 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 -- http://mail.python.org/mailman/listinfo/python-list