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 17:14:18 CEST 2009


It looks like what is needed here are a kind of "mutable float".  Is
there a simple way of creating such a type?  I don't mind changing the
value through x.value = 1.23 instead of x = 1.23... :)

On Apr 14, 3:03 pm, Eric.Le.Bi... at spectro.jussieu.fr wrote:
> 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