How to store the reference of a dictionary element ?
Terry Reedy
tjreedy at udel.edu
Thu Dec 19 17:38:34 EST 2002
More information about the Python-list mailing list
Thu Dec 19 17:38:34 EST 2002
- Previous message (by thread): How to store the reference of a dictionary element ?
- Next message (by thread): How to store the reference of a dictionary element ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Alfredo P. Ricafort" <alpot at mylinuxsite.com> wrote in message news:mailman.1040293705.17998.python-list at python.org... > On Thu, 2002-12-19 at 15:46, Terry Reedy wrote: > > inputData['childKey'] = [attribute1, attribute2,inputData['parentKey']] > This won't do. If the value list of inputData['parentKey'] is changed > the inputData['childKey'] will not see the changes. Any newbie who wants to write correct code should ignore this. Two references to the same object are two references to the *same object*. If the object is mutable and is changed, then the change is visible no matter how you access it. This is a common 'gotcha' for newbies who mistakenly think that assignment creates a copy, which it does not. To illustrate: >>> d={} >>> d['p']=[1,2] >>> d['c']=[3,4,d['p']] # equivalent to what I wrote and quoted above >>> id(d['p']), id(d['c'][2]) (7953824, 7953824) >>> d['p'].append(5) # a change to the parent value list >>> d['c'][2] # access via the child *does* see the change [1, 2, 5] Terry J. Reedy
- Previous message (by thread): How to store the reference of a dictionary element ?
- Next message (by thread): How to store the reference of a dictionary element ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list