You really shouldn't. The difference between float
and Decimal
is very important, and you don't want to hide that difference from readers of your code.
If you're using a huge number of Decimal
constants, some people like to do this:
from decimal import Decimal as D
c0 = D('273.15')
f0 = D('491.67')
c_ms = D('299792485')
# etc.
Another trick I've seen occasionally is to create a block of constants en masse:
constants = '''
273.15 # c0
491.67 # f0
299792485 # c_ms
'''
constants = (line.split('#', 1)[0].strip() for line in constants.splitlines())
c0, f0, c_ms = (Decimal(line) for line in constants if line)
If you really want to push it, you can parse out the names and use globals()[name]
to set them, but that's pretty icky.
If you want to go really over the top, you can write an import hook that changes every float literal into a Decimal constructor. This project is a starting point, although IIRC it only works in Python 3.3 to 3.5 or so out of the box.
But really, it's probably better to just get your editor or IDE to help you. That makes the code easier to write, without making it harder to read or misleading.