One of the lovely things about Python is that we have the capability to avoid issues such as the vagaries of the floating point type with libraries such as decimal and fractions. This is wonderous to me but comes with an issue that I suspect is limiting its usage. That issue is that you have to litter your code with constructors of those types, e.g.

 

```

a = 0.1

b = 0.2

c = 0.3

a + b == c # This doesn’t work but it is easy to forget why

```

vs:

```

from decimal import *  # I know why this is bad but it comes straight from the examples

a = Decimal("0.1")  # Needs to be a string to avoid being temporarily a float

b = Decimal("0.2")  # Ditto

c = Decimal("0.3")  # Ditto

a + b == c  # Magic this works!

```

 

While this works it is not very friendly to users and there will often be cases where people forget to quote the values, etc. and there are similar issues with using the fractions library and I am sure with others. I suspect that this is why the decimals library, and some others, is not used as widely as it possibly should be.

 

Wouldn’t it be possible to have something along the lines of:

 

```

from decimal import TreatFloatsAsDecimal

@TreatFloatsAsDecimal

a = 0.1  # These are all now decimals

b = 0.2

c = 0.3

a + b == c # This now works

```

Less obviously this sort of approach could also apply to making all integers into Fractions or other base types into something else.

 

I do know that this goes against the explicit is better than implicit so an alternative might be to have a flexible base modifier, something like:

```

from decimal import DecimalBase as D

 

a = 0D0.1  # These are all now decimals

b = 0D0.2

c = 0D0.3

a + b == c # This now works

```

 

Since anything like this would require overriding at least part of the base interpreter I do not think that this is a suitable one for an external PyPi library or at least some hooks in the interpreter would be required to make it possible.

 

Any thoughts, feedback, interest, expressions of horror? Or is this already there and I have missed it?

 

Steve Barnes