I am never going to complain about Python again

Neil Cerutti neilc at norwich.edu
Thu Oct 10 14:49:40 EDT 2013


On 2013-10-10, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Oct 10, 2013 at 11:48 AM, Neil Cerutti <neilc at norwich.edu> wrote:
>> Woah. I thought I was going by what the docs say:
>>
>>   Python fully supports mixed arithmetic: when a binary
>>   arithmetic operator has operands of different numeric types,
>>   the operand with the ?narrower? type is widened to that of the
>>   other, where integer is narrower than floating point, which is
>>   narrower than complex. Comparisons between numbers of mixed
>>   type use the same rule. [2] The constructors int(), float(),
>>   and complex() can be used to produce numbers of a specific
>>   type.
>>
>> [...]
>>
>>   [2] Not for complex numbers. Instead convert to floats using
>>      abs() if appropriate.
>>
>> I guess the "if appropriate" part eluded my eye. When *is* it
>> appropriate? Apparently not during an equal test.
>
> If you click on the footnote, it takes you to:
>
> [2]As a consequence, the list [1, 2] is considered equal to [1.0,
> 2.0], and similarly for tuples.
>
> The text that you have mistakenly identified as the footnote is
> actually part of the key to the "Notes" column of the numeric
> operations table, where it is referred to by the "x % y" and
> "divmod(x, y)" operations.  Specifically, it warns of this error:
>
>>>> 3j % 2j
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: can't mod complex numbers.

Doh!

Thanks, for that, and for the corrections. I could have avoided
all this by testing it correctly in the REPL, too.

I'll click on those footnotes instead of scanning to them from
now on.

-- 
Neil Cerutti



More information about the Python-list mailing list