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