I would like to modify the value of a float instance as follow:
>>>%paste
class MyFloat(float):
def foo(self):
self = self + 1
>>> f = MyFloat(41)
>>> f.foo()
>>> f
41.0
Unfortunately self += 1
does not work
I assume the builtins objects are immutable and even f.__add(1)
will return a new instance of float. Am I right?
I need a container that represents the latched value of measure acquired from a USB device. Once the value is latched, I get a float with additional methods such as an Update
method to fetch a new measurement from de device or units
that returns the unit of the measurement. I would like to update the content of the instance when I execute the Update
method. If floats are immutable and there is no way to reverse this. I will probably need to rename my Update
in GetNewMeasurement
which will return a new instance of the actual object. The second question Why do I need an Update method?
might be asked next. A measurement is linked to several parameters: device address, register name, type of measurement, configuration of the device when the measurement was acquired... To get a new measurement in the exact same conditions, I need to know all these parameters.
On the reference manual of Python on the Standard type hierarchy, I an read that numbers.Number
are immutables. I don't know if it applies to floats too and whether numbers.Real
is the same as float
. Perhaps it is possible to have a derived version of float
that is not immutable.