[Python-ideas] isinstance(Decimal(), Real) -> False?

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Aug 28 12:49:15 CEST 2013


On 28 August 2013 11:22, Paul Moore <p.f.moore at gmail.com> wrote:
> On 28 August 2013 11:15, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
>>
>> There's also no way to arithmetic with int/Fraction real and imaginary
>> components (e.g. gaussian integers etc.) but these are still instances
>> of Complex.
>
> The difference is that there is no implicit conversion from Decimal to
> float:
>
>>>> from decimal import Decimal as D
>>>> D('1.5') + 2.3
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
>>>> from fractions import Fraction as F
>>>> F(1,2) + 2.3
> 2.8
>
> IIRC, that's the reason Decimal is treated specially here - maybe it's not a
> sufficiently good reason (I don't have an opinion on that) but from what
> little I recall of the discussions around the time, it's the key
> distinguishing feature of Decimal in these matters...

Why shouldn't there be implicit conversion in Decimal arithmetic?
There already is for all the other numeric types. Also explicit
conversion seems to blocked in some cases. This one in particular
bothers me (since it's often desirable to get a decimal representation
of a fraction):

$ python3.3
Python 3.3.0 (default, Sep 29 2012, 17:14:58)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal as D
>>> from fractions import Fraction as F
>>> D(F(1, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: conversion from Fraction to Decimal is not supported


Oscar


More information about the Python-ideas mailing list