Python Values ​​with Units

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?
+3
source share
5 answers

Perhaps you are looking for something like this:

class UnitFloat(float):

    def __new__(self, value, unit=None):
       return float.__new__(self, value)

    def __init__(self, value, unit=None):
        self.unit = unit


x = UnitFloat(35.5, "cm")
y = UnitFloat(42.5)

print x
print x.unit

print y
print y.unit

print x + y

Productivity:

35.5
cm
42.5
None
78.0
+8
source

__new__ ( " ", __init__ - "" ), float __new__ , , . float __init__ ( -op). :

class floatwithunit(float):

    def __new__(cls, value, *a, **k):
        return float.__new__(cls, value)

    def __init__(self, value, *args, **kwargs):
        self.unit = kwargs.pop('unit', None)

    def __str__(self):
        return '%f*%s' % (self, self.unit)

a = floatwithunit(1.,unit=1.)

print a

1.000000*1.0.

+6

,

class floatwithunit(float):

def floatwithunit(float):
+1
. __new__ , , :
class FloatWithUnit(float):
    def __new__(cls, *args, **kwargs):
        # avoid error in float.__new__
        # the original kwargs (with 'unit') will still be passed to __init__
        if 'unit' in kwargs:
            kwargs.pop('unit')
        return super(FloatWithUnit, cls).__new__(cls, *args, **kwargs)

    def __init__(self, *args, **kwargs):
        self.unit = kwargs.pop('unit') if 'unit' in kwargs else None
        super(FloatWithUnit, self).__init__(*args, **kwargs)
0

, , kwargs None (), , 'unit'.

 if kwargs and 'unit' in kwargs:

:

don't pass kwargs to __oldinit__
-2

Source: https://habr.com/ru/post/1724824/


All Articles