Modifying the value of a float-like object

Eric.Le.Bigot at spectro.jussieu.fr Eric.Le.Bigot at spectro.jussieu.fr
Wed Apr 15 04:05:33 EDT 2009


Ben F., you're right on the money!  You expressed exactly what I'm
looking for.  Why should I want this?  because the place in the code
where (foo, baz) is calculated has _no idea_ of what foo and baz are,
of where they were defined, etc.; on the other hand, the floatref
class can keep track of the values that were constructed, and I'm
looking for a way of modifying them from its perspective
(floatref.change_value(index, new_value)).

Steven, t

To Steven: Thank you for your suggestion about checking
UserString.MutableString.
I do understand that mutable objects can be surprising, but, again,
Python programmers constantly use such objects (lists, dicts,....): I
don't think that manipulating a mutable float is more of a problem
than using a list, for a Python programmer.
As for your idea of "straight-forward interval arithmetic", it's a
good one, but I'd have to redefine lots of functions from the math
module, to use them explicitly in my code, etc.: this is heavy; I was
looking for a light-weight alternative, where all calculations are
expressed in Python as if all numbers were regular floats, and where
you don't have to redefine any mathematical operation.  In other
words, I'm looking for a robust way of implementing the derive()
function of Peter without knowing anything about which function uses
which "numbers with an uncertainty": the equivalent of Peter's derive
() would simply successively change all "numbers with uncertainty"
defined so far and see how the result of a given calculation varies--
the variables that are not used in the calculated expression don't
change the result, for instance.  I understand that this is
computationally inefficient (too many variables might be changed), but
I don't need speed, only robustness in the propagation of errors, and
a simple and very legible calculation code, and ideally with only a
few lines of Python.

Anyway, Steven gave me hope that some kind of mutable float is
possible in Python, and Ben perfectly expressed my need.  Again,
alternatively, any module that handles in a convenient manner
calculations with error propagation would be great, but I have not
found any single such module!


On Apr 15, 6:17 am, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
> Steven D'Aprano <ste... at REMOVE.THIS.cybersource.com.au> writes:
> > On Tue, 14 Apr 2009 06:03:58 -0700, Eric.Le.Bigot wrote:
> > > 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.
>
> > Why is that the goal? If you want to change the value of x, just
> > change the value of x.
>
> As you're aware (but Eric may not be), Python doesn't have “change the
> value of x”. The closest would be to re-bind the name ‘x’ to a different
> value, which would not be what Eric is asking for.
>
> If I understand correctly, Eric wants something with the following
> behaviour:
>
>     >>> foo = [floatref(3.14), floatref(1.41)]
>     >>> bar = foo[0]
>     >>> baz = foo[1]
>     >>> foo
>     [3.14, 1.41]
>     >>> (bar, baz)
>     (3.14, 1.41)
>
>     >>> foo[1].changevalue(1.62)
>     >>> foo
>     [3.14, 1.62]
>     >>> (bar, baz)
>     (3.14, 1.62)
>
> and is asking how to get such a ‘floatref’.
>
> --
>  \        “If you go parachuting, and your parachute doesn't open, and |
>   `\        you friends are all watching you fall, I think a funny gag |
> _o__)             would be to pretend you were swimming.” —Jack Handey |
> Ben Finney




More information about the Python-list mailing list