Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16913
| Date | 2011-12-09 12:43 +0000 |
|---|---|
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
| Subject | Re: tracking variable value changes |
| References | <jbr607$eer$1@news.jpl.nasa.gov> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3470.1323434593.27778.python-list@python.org> (permalink) |
On 12/08/2011 08:17 PM, Catherine Moroney wrote:
> Hello,
>
> Is there a way to create a C-style pointer in (pure) Python so the
> following code will reflect the changes to the variable "a" in the
> dictionary "x"?
>
> For example:
>
> >>> a = 1.0
> >>> b = 2.0
> >>> x = {"a":a, "b":b}
> >>> x
> {'a': 1.0, 'b': 2.0}
> >>> a = 100.0
> >>> x
> {'a': 1.0, 'b': 2.0} ## at this point, I would like the value
> ## associated with the "a" key to be 100.0
> ## rather than 1.0
>
> If I make "a" and "b" numpy arrays, then changes that I make to the
> values of a and b show up in the dictionary x.
>
> My understanding is that when I redefine the value of "a", that Python
> is creating a brand-new float with the value of 100.0, whereas when I
> use numpy arrays I am merely assigning a new value to the same object.
>
> Is there some way to rewrite the code above so the change of "a" from
> 1.0 to 100.0 is reflected in the dictionary. I would like to use
> simple datatypes such as floats, rather than numpy arrays or classes.
> I tried using weakref's, but got the error that a weak reference cannot
> be created to a float.
>
> Catherine
Not sure if it's exactly pure python but Traits can actually do this
https://github.com/enthought/traits
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
tracking variable value changes Catherine Moroney <Catherine.M.Moroney@jpl.nasa.gov> - 2011-12-08 12:17 -0800
Re: tracking variable value changes Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-08 14:50 -0700
Re: tracking variable value changes Ben Finney <ben+python@benfinney.id.au> - 2011-12-09 09:06 +1100
Re: tracking variable value changes Arnaud Delobelle <arnodel@gmail.com> - 2011-12-08 22:10 +0000
Re: tracking variable value changes Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-09 12:43 +0000
Re: tracking variable value changes alex23 <wuwei23@gmail.com> - 2011-12-11 21:45 -0800
Re: tracking variable value changes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-10 00:41 +0000
csiph-web