floatref

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Tue Jul 13 21:53:14 EDT 2010


On Tue, 13 Jul 2010 19:26:34 +0200, Roald de Vries wrote:

> 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.



-- 
Steven



More information about the Python-list mailing list