Mixing Decimal and float

Mark Dickinson dickinsm at gmail.com
Wed Jun 2 06:22:11 EDT 2010


On Jun 2, 9:24 am, "B.V." <bv.try... at gmail.com> wrote:
> Hi,
>
> In order to solve some issues due to operations between Decimal and
> float, we wanted to implement a class that inherits from both float
> and Decimal.
>
> Typically, we wrote:
>
> class Float(Decimal, float):

Can you explain exactly what issues you want to solve, and how you
want your Float class to behave?  Do I understand correctly that you
want your Float class to be able to represent both floats and
Decimals?

> But we also need to do:
> isinstance(Float('1'), float) == True
> isinstance(Float('1'), Decimal) == True

Can you explain why you need this?

Should isinstance(Float('1.1'), float) and isinstance(Float('1.1'),
Decimal) also both be true, or would only one of those be true?  (And
by the way, what value would Float('1.1') have?  float('1.1') and
Decimal('1.1') are different values.)

I don't think your approach can succeed;  I'd suggest just subclassing
'object' and abandoning the 'isinstance' requirements.  Or perhaps
creating a subclass of Decimal that interacts nicely with floats.  You
might also want to investigate the numbers ABC, though that's new in
Python 2.6.

--
Mark



More information about the Python-list mailing list