From: Roald de Vries on
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
From: Gary Herron on
On 07/13/2010 10:26 AM, 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.
>
> Roald

Huh? I must be missing something here. Isn't this what you use a
variable for:

sharedFloat = 123

and later

sharedFloat = 99

The variable sharedFloat could exit as a global (in some module) or as a
instance variable in some shared object or anywhere else you want to
store it.

As a module attribute:

import Shared
Shared.sharedFloat = 123

Or as a class attribute:

class Shared:
sharedFloat = 0

Shared.sharedFloat = 123

or as an instance attribute:

class Shared:
def __init__(self):
self.sharedFloat = 0

sharedObject = Shared()
sharedObject.sharedFloat = 123



Gary Herron








--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

From: Gary Herron on
On 07/13/2010 03:02 PM, Roald de Vries wrote:
> Hi Gary,
>
> On Jul 13, 2010, at 8:54 PM, Gary Herron wrote:
>> On 07/13/2010 10:26 AM, 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.
>>>
>>> Roald
>>
>> Huh? I must be missing something here. Isn't this what you use a
>> variable for:
>
> Maybe I didn't explain well:
>
> >>> shared_var = 1.0
> >>> x.var = shared_var
> >>> y.var = shared_var
> >>> x.var = 2.0
> >>> y.var
> 1.0
>
> I wanted y.var and x.var to point to the same value, so that always
> x.var == y.var. So that the last line becomes:
>
> >>> y.var
> 2.0
>
> Cheers, Roald

Please keep responses and further discussions on
list.python-list(a)python.org
instead of using private emails.

Python does not have pointers, so if I take your wording"y.var and x.var
to point to the same value" literally, then the answer is NO Python
does not do that.

However, Python does have references all over the place, so you can
achieve something similar in many ways.

If x and y in your example code are instances of a class, than look into
using a property for x.var and y.var. A property is a thing that looks
like an attribute, (that would be var in x.var and y.var), but which
really executes getter/setter code when accessed. That getter/setter
code would then access/set the value in shared_var:


shared_var = 123

class Thing(object):
def get_var(self):
return shared_var
def set_var(self, v):
global shared_var
shared_var = v

var = property(get_var, set_var)


x = Thing()
y = Thing()

print x.var, y.var # prints: 123 123
x.var = 99
print x.var, y.var # prints: 99 99


--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
From: Alf P. Steinbach /Usenet on
* Gary Herron, on 14.07.2010 01:26:
> On 07/13/2010 03:02 PM, Roald de Vries wrote:
>> Hi Gary,
>>
>> On Jul 13, 2010, at 8:54 PM, Gary Herron wrote:
>>> On 07/13/2010 10:26 AM, 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.
>>>>
>>>> Roald
>>>
>>> Huh? I must be missing something here. Isn't this what you use a
>>> variable for:
>>
>> Maybe I didn't explain well:
>>
>> >>> shared_var = 1.0
>> >>> x.var = shared_var
>> >>> y.var = shared_var
>> >>> x.var = 2.0
>> >>> y.var
>> 1.0
>>
>> I wanted y.var and x.var to point to the same value, so that always
>> x.var == y.var. So that the last line becomes:
>>
>> >>> y.var
>> 2.0
>>
>> Cheers, Roald
>
> Please keep responses and further discussions on
> list.python-list(a)python.org
> instead of using private emails.

Seconded. I didn't see that posting.


> Python does not have pointers, so if I take your wording"y.var and x.var
> to point to the same value" literally, then the answer is NO Python does
> not do that.

This is just a terminological issue. Saying "Python does not have pointers" is
highly misleading in response to the OP's statement. It's easy enough to
understand what he means. E.g., in the Java language specification "pointer" has
a suitable meaning. And in general, the term "pointer" encompasses far more than
the concept of a C or Pascal pointer, e.g., in C++ it includes offset-like
things called member pointers. You can't represent or usefully think of a C++
member pointer as a C or Pascal pointer. It isn't useful on its own. So,
considering C++ and Java, the general pointer notion is something that refers,
however obliquely. You chose a specific meaning of "pointer" where the OP's
statement does not make sense, but presumably the OP is explaining his needs in
terms of a more general meaning of "pointer"; assuming anything else is silly.


> However, Python does have references all over the place, so you can
> achieve something similar in many ways.
>
> If x and y in your example code are instances of a class, than look into
> using a property for x.var and y.var. A property is a thing that looks
> like an attribute, (that would be var in x.var and y.var), but which
> really executes getter/setter code when accessed. That getter/setter
> code would then access/set the value in shared_var:
>
>
> shared_var = 123
>
> class Thing(object):
> def get_var(self):
> return shared_var
> def set_var(self, v):
> global shared_var
> shared_var = v
>
> var = property(get_var, set_var)
>
>
> x = Thing()
> y = Thing()
>
> print x.var, y.var # prints: 123 123
> x.var = 99
> print x.var, y.var # prints: 99 99

This code goes through hoops to /hide/ the fact of the sharing.

Rather, I'd make that as explicit as possible.

Like,

x = {"sharedVar": 123}
y = x

The one won't be surprised when changing x["sharedVar"] also changes y["sharedVar"].


Cheers & hth.,

- Alf


--
blog at <url: http://alfps.wordpress.com>
From: Steven D'Aprano on
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