Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18911

Re: copy on write

Date 2012-01-13 13:07 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: copy on write
References <4F101684.2060502@itccanarias.org> <4F101938.1030806@itccanarias.org>
Newsgroups comp.lang.python
Message-ID <mailman.4710.1326456427.27778.python-list@python.org> (permalink)

Show all headers | View raw


Eduardo Suarez-Santana wrote:
> El 13/01/12 11:33, Eduardo Suarez-Santana escribió:
>> I wonder whether this is normal behaviour.
>>
> Even simpler:
>
> $ python
> Python 2.7.2 (default, Oct 31 2011, 11:54:55)
> [GCC 4.5.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> r={'a':1};
> >>> d={};
> >>> d['x']=r;
> >>> d['y']=r;
> >>> d['x']['a']=3
> >>> d['y']
> {'a': 3}
> >>>
>
yes it is.

 >>> d['x']=r;
 >>> d['y']=r;

means that both d['x'] and d['y'] name the same object r. If you change 
r, you'll see these changes wheter using d['x'] or d['y'].

The operator '=' does not copy objects, it binds an object to a name, 
and an object can have multiple names.
Use the dictionary copy method to copy a dictionary:

d['x'] = r.copy()

JM

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: copy on write Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-01-13 13:07 +0100

csiph-web