floatref

R

Roald de Vries

Hi all,

I have two objects that should both be able to alter a shared float.
So i need something like a mutable float object, or a float reference
object. Does anybody know if something like that exists? I know it's
not hard to build, but I have a feeling that there should be a
standard solution to it.

Roald
 
S

Steven D'Aprano

Hi all,

I have two objects that should both be able to alter a shared float. So
i need something like a mutable float object, or a float reference
object. Does anybody know if something like that exists? I know it's not
hard to build, but I have a feeling that there should be a standard
solution to it.


One standard solution would be to wrap the float in a class as an
attribute. Another is to put it in a list:

myvalue = [3.1415]
pi = myvalue
myvalue[0] = 3.0
assert pi[0] == 3.0

A third standard solution is to create a mutable float using delegation.
See the MutableStr class in the standard library for hints.


An even better solution is to avoid the idiom of shared state in the
first place. Whenever people say "I need shared data", the chances are
very good that they don't *need* it at all, but merely *want* it because
that's the idiom they're used to.

But if you really need it, you can make it.
 
R

Roald de Vries

One standard solution would be to wrap the float in a class as an
attribute.

E.g.: a class FloatWrapper with a get and set method.
Advantages: transparent, easy to implement
Disadvantages: for 'f = FloatWrapper' you have to use 'f.set(1.0)'
instead of 'f = 1.0', which reads less easily (I think), and a mistake
like assigning with 'f = 1.0' is easily made.
Another is to put it in a list:

myvalue = [3.1415]
pi = myvalue
myvalue[0] = 3.0
assert pi[0] == 3.0

I thought about something like that, but it's a bit misleading,
because the value is actually a list.
An even better solution is to avoid the idiom of shared state in the
first place. Whenever people say "I need shared data", the chances are
very good that they don't *need* it at all, but merely *want* it
because
that's the idiom they're used to.

I want to simulate a distributed algorithm, in which components can
only communicate through shared state.

Cheers, Roald
 
R

Roald de Vries

One standard solution would be to wrap the float in a class as an
attribute.

I think the nicest solution is to do this with a wrapping descriptor
class.

Thanks for your input!

Cheers, Roald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top