2

Is it possible to change the "default" datatypes in python? Such that when I declare a variable with a value 0.2, it will be a decimal.Decimal in stead of a float? The only thing I could think of was this

>>> from decimal import Decimal
>>> float = Decimal
>>> float(".2")+float(".1")
Decimal('0.3')

Which is kinda the behaviour I wanted, but not quite:

>>> .2+.1
0.30000000000000004

I expected similar results as with float(".2")+float(".1"), but I guess x=.1 is handled differently than x=float(".1").

Is there any way to achieve the first behaviour without excplicitly calling float()?

Låne Book
  • 73
  • 7
  • 1
    No; you cannot change the behavior of a literal. – chepner May 25 '20 at 16:14
  • 1
    Does this answer your question? [Can literals in Python be overridden?](https://stackoverflow.com/questions/19083160/can-literals-in-python-be-overridden) – Tomerikoo May 25 '20 at 16:16

0 Answers0