[Python-Dev] return type of __complex__

Chris Angelico rosuav at gmail.com
Sun Oct 21 14:06:45 CEST 2012


On Sun, Oct 21, 2012 at 10:08 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Sun, 21 Oct 2012 22:02:17 +1100
> Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> In Python 3.3, math.pow is the builtin pow. Presumably for backwards
>> compatibility reasons when they were different? If so, it was a LONG
>> time ago:
>>
>> [steve at ando ~]$ python1.5
>> Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
>> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>> >>> import math
>> >>> math.pow
>> <built-in function pow>
>
> You are being too optimistic:
>
>>>> pow
> <built-in function pow>
>>>> math.pow
> <built-in function pow>
>>>> pow is math.pow
> False

The one in math doesn't take three args.

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win32
>>> math.pow(1,2,3)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    math.pow(1,2,3)
TypeError: pow expected 2 arguments, got 3

I conducted a brief survey of a non-technical nature (that is to say,
I rambled to my brother and listened to what he said in response), and
he's of the opinion that many programmers would be surprised and
confused by the sudden appearance of complex numbers in programs that
weren't using them. On the flip side, it's kinda like a NaN - you
don't get an error immediately, but you'll either see it in your
output or get an exception later on.

His suggestion was that an expression unexpectedly returning a complex
should issue a warning. Warnings aren't really useful in Python as
most people have them off, but would there be any way that this sort
of thing could be caught by a linter? It'd probably require some
fairly deep code analysis to figure out that you're taking the square
root of something that could be negative, though.

ChrisA


More information about the Python-Dev mailing list