* 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-l...@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>
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to