Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'value,': 0.04; 'pointer': 0.05; 'python': 0.08; '"a"': 0.09; '>>>>': 0.09; 'array.': 0.09; 'float': 0.13; '"b"': 0.16; 'arrays,': 0.16; 'brand-new': 0.16; 'numpy': 0.16; 'subject:changes': 0.16; 'subject:variable': 0.16; 'ugly.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'of.': 0.18; 'cc:no real name:2**0': 0.20; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'assigning': 0.23; 'dictionary': 0.23; 'cc:2**0': 0.24; 'creating': 0.25; 'code': 0.25; 'variable': 0.28; 'putting': 0.28; 'effect': 0.28; 'message- id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'array': 0.30; 'arrays': 0.30; 'object.': 0.30; 'reflect': 0.31; 'values': 0.32; 'point,': 0.32; 'thu,': 0.32; 'changes': 0.32; 'objects': 0.32; 'sort': 0.33; 'rather': 0.33; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'received:74.125.82': 0.35; 'bound': 0.37; 'variables': 0.37; 'but': 0.37; 'except': 0.37; 'list,': 0.37; 'hello,': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'some': 0.38; "it's": 0.40; '2011': 0.61; 'you.': 0.63; 'show': 0.67; 'directly.': 0.68; 'subject:value': 0.84; 'story,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=Zgriy1cSfzaxvxCILchz5sCydVFB7/0SBdtqQOkWRB4=; b=fuEaW95N4mblfiSPXh67UEyxFx3Ic2OdMFA4V5uuihKGTLlPbblyHBLNnLmj6vp4ah zKVbVpIpUYGcUgtgBf3llHcC84JKMScGzIrYhRKEq6onaWc/+8qJAHTXdMeLHQ6bSjkC bxxckTTemb+xDekmmyjTdJt2+1PpN9mlz3ab8= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 8 Dec 2011 14:50:38 -0700 Subject: Re: tracking variable value changes To: Catherine Moroney Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323381077 news.xs4all.nl 6902 [2001:888:2000:d::a6]:49064 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16881 On Thu, Dec 8, 2011 at 1:17 PM, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the follow= ing > code will reflect the changes to the variable "a" in the > dictionary "x"? > > For example: > >>>> a =3D 1.0 >>>> b =3D 2.0 >>>> x =3D {"a":a, "b":b} >>>> x > {'a': 1.0, 'b': 2.0} >>>> a =3D 100.0 >>>> x > {'a': 1.0, 'b': 2.0} =A0 ## at this point, I would like the value > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ## associated with the "a" ke= y to be 100.0 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ## rather than 1.0 > > If I make "a" and "b" numpy arrays, then changes that I make to the value= s > 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. Sort of. In the code above, you are binding a and x["a"] to the same float object. Then when you do "a =3D 100.0", you are rebinding a but not x["a"]. In the case of arrays it's the same story, except that you can also *modify* the contents of the array instead of rebinding to a new array. In that case both a and x["a"] are still bound to the original array, the contents of which have changed. You can get the same effect with a float by putting it in a container object and binding both variables to the same container objects rather than to the float directly. Then, to change the value, change the contents of the container object. What you use as a container object is up to you. Some use a 1-element list, although I find that ugly.