Modifying the value of a float-like object

Eric.Le.Bigot at spectro.jussieu.fr Eric.Le.Bigot at spectro.jussieu.fr
Tue Apr 14 15:03:58 CEST 2009


Hello,

Is there a way to easily build an object that behaves exactly like a
float, but whose value can be changed?  The goal is to maintain a list
[x, y,…] of these float-like objects, and to modify their value on the
fly (with something like x.value = 3.14) so that any expression like "x
+y" uses the new value.

I thought of two solutions, both of which I can't make to work:

1) Use a class that inherits from float.  This takes care of the
"behave like float" part.  But is it possible to change the value of
the float associated with an instance?  That is, is it possible to
do:  "x = MyFloat(1.23); x.change_value(3.14)" so that x's float value
becomes 3.14?

2) The other possibility I thought of was: use a class that defines a
'value' member (x.value).  This takes care of the "value can be
changed" part.  But is it possible/easy to make it fully behave like a
float (including when passed to functions like math.sin)?

Alternatively, I'd be happy with a way of handling numerical
uncertainties in Python calculations (such as in "calculate the value
and uncertainty of a*sin(b) knowing that a=3.0 +/- 0.1 and b=1.00 +/-
0.01").

Any idea would be much appreciated!



More information about the Python-list mailing list