[Python-ideas] Yet another sum function (fractions.sum)

Steven D'Aprano steve at pearwood.info
Thu Aug 22 03:48:35 CEST 2013


On 22/08/13 03:32, Stephen J. Turnbull wrote:
> random832 at fastmail.us writes:

>   > Isn't that unpythonic? I mean, it's like doing type checking
>
> No, it's not, and it's not.  In Python, turning 1 into 1 + 0j is a
> type conversion[1]:
>
>>>> 1 is 1 + 0j
> False
>>>> 1 is 1
> True

Steve, not only is that a dubious, implementation-dependent test, as you recognise in the footnote, but it doesn't even demonstrate the fact you are trying to demonstrate. To whit:

py> [] is []
False

and therefore [] and [] are different types... not.

The test I think you want is:

py> type(1) is type(1+0j)
False


As far as whether some hypothetical sqrt method should return a complex number or raise an exception, I'd like to point out that as of Python 3.x, math.sqrt is now the odd-man-out:

py> (-25)**0.5
(3.061515884555943e-16+5j)
py> cmath.sqrt(-25)
5j
py> math.sqrt(-25)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: math domain error



-- 
Steven


More information about the Python-ideas mailing list