I need to track units by float and int values ββin Python, but I do not want to use an external package such as a quantity or others, because I do not need to perform operations on the values. Instead, all I want is the ability to define floats and ints that have a unit attribute (and I don't want to add a new dependency for something so simple). I tried:
class floatwithunit(float):
__oldinit__ = float.__init__
def __init__(self, *args, **kwargs):
if 'unit' in kwargs:
self.unit = kwargs.pop('unit')
self.__oldinit__(*args, **kwargs)
But this does not work at all:
In [37]: a = floatwithunit(1.,unit=1.)
TypeError Traceback (most recent call last)
/Users/tom/<ipython console> in <module>()
TypeError: float() takes at most 1 argument (2 given)
Any suggestions?
source
share