-3

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 1
    Not all builtin types are immutable, but floats certainly are. *"Am I right?"* - why don't you read the docs? Why do you think you need this? – jonrsharpe Dec 13 '15 at 12:36
  • You should specifiy why you would need this, as it doesn't seem practical to overload the float class for just having another float class. – Randrian Dec 13 '15 at 12:46
  • Why not just put the float in a mutable container, then? E.g. a list, although if you really wanted to write a mutable float look at [*"emulating numeric types"*](https://docs.python.org/2/reference/datamodel.html#emulating-numeric-types). That detail isn't required to get an answer to *"are floats immutable"*, but might help get an answer that actually solves the underlying problem. – jonrsharpe Dec 13 '15 at 13:16
  • @jonrsharpe Emulating a float container is lots of work. It looks much easier to inherit the basic float type if I don't want to change all the basic attributes of a float. Am I wrong? – nowox Dec 13 '15 at 14:34
  • It's a lot of work, but if you want to make a mutable float it's necessary, whether or not you inherit from `float`. Otherwise you're inevitably using the inherited implementations of e.g. `__add__` and getting immutable behaviour. Putting the float in a list would be considerably simpler. – jonrsharpe Dec 13 '15 at 14:35
  • I don't get the latter point. How would you solve the problem by putting the float in a list? – nowox Dec 13 '15 at 14:36
  • Because lists are mutable; if you always access the float via a list's index, then changes will be seen via other references to that list, which is the actual behaviour you seem to want. You could alternatively use a dictionary if you've a sensible name for the object (e.g. instead of `f` you'd access it at `some_map['f']`). – jonrsharpe Dec 13 '15 at 14:44
  • I cannot do `a = [2.0]` followed by `b = a + 2`. Even if I want to do it I need to overload the `__add__` of the list which is the same that building my own float where `def __add(self, other): return MyFloat(self.__value.__add__(other))` – nowox Dec 13 '15 at 14:47
  • No, you also have to change the way you access it, so you'd have e.g. `b = a[0] + 2` (or `b = [a[0] + 2]` to retain the mutable behaviour). A dictionary is good here because a key can generally be more meaningful than an index. If you could show a less abstract [mcve] that might help. And note that, as you should know by now, you need to include `@username` to notify people you're responding. – jonrsharpe Dec 13 '15 at 18:17

1 Answers1

0

Float is immutable, you are right.

dir(float)
>>>[, 'real']

You can work with that attribute to return a new, incremented, reference.

edd
  • 1,307
  • 10
  • 10