Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'distinct': 0.05; 'python': 0.08; '>>>>': 0.09; 'programmers.': 0.09; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; '"+="': 0.16; "'a'": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:copy': 0.16; 'sublist': 0.16; 'wrote:': 0.16; '>>>': 0.18; '3.2': 0.18; 'jan': 0.19; 'seems': 0.19; 'header:In-Reply- To:1': 0.22; 'object,': 0.24; 'modify': 0.25; 'pm,': 0.26; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'fri,': 0.30; 'strings,': 0.30; 'pointing': 0.32; 'objects': 0.32; 'list': 0.32; "can't": 0.32; 'to:addr:python-list': 0.33; 'object': 0.33; 'creates': 0.34; 'normally': 0.34; 'but': 0.37; "there's": 0.37; 'received:google.com': 0.37; 'steven': 0.38; 'received:209.85': 0.38; 'point': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'difference': 0.40; 'types': 0.61; '(always': 0.84; '11:10': 0.84; 'sorely': 0.84; 'subject:write': 0.84; 'windows)': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=psb66O65yjaasF7LhkhllRBi/6nWEvJ2y8gujlu5qts=; b=jqBhp90MPGY+w6PSJPEc2omydabZlK3bOqxRvIxaUfms0CrxCVRVdd99TwUJRVCJWq xsH/GSWNMeVut9I6QKjl1sGp6c4SldgApdjlx3oZbrv9N5Vk8r59Ew3xz9ElYNVQftzJ LkUzhuea9VQJwBJapbbUBHooVOU37PXhNW5bI= MIME-Version: 1.0 In-Reply-To: <4f101f45$0$29999$c3e8da3$5496439d@news.astraweb.com> References: <4f101f45$0$29999$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 13 Jan 2012 23:30:56 +1100 Subject: Re: copy on write From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326457859 news.xs4all.nl 6946 [2001:888:2000:d::a6]:47553 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18914 On Fri, Jan 13, 2012 at 11:10 PM, Steven D'Aprano wrote: >>>> z =3D [x, y] =A0# z is a list containing the same sublist twice >>>> z[0].append(23) >>>> print z > [[42, 23], [42, 23]] > > When you work with floats, ints or strings, you don't notice this because > those types are immutable: you can't modify those objects in place. So > for example: > >>>> a =3D 42 =A0# binds the name 'a' to the object 42 >>>> b =3D a =A0# a and b point to the same object >>>> a +=3D 1 =A0# creates a new object, and binds it to a >>>> print b =A0# leaving b still pointing to the old object > 42 I was about to say that it's a difference between ".append()" which is a method on the object, and "+=3D" which is normally a rebinding, but unfortunately: >>> a=3D[] >>> b=3Da >>> a+=3D[1] >>> a [1] >>> b [1] >>> b+=3D[2] >>> a [1, 2] >>> a [1, 2] >>> a=3Da+[3] >>> a [1, 2, 3] >>> b [1, 2] (tested in Python 3.2 on Windows) It seems there's a distinct difference between a+=3Db (in-place addition/concatenation) and a=3Da+b (always rebinding), which is sorely confusing to C programmers. But then, there's a lot about Python that's sorely confusing to C programmers. ChrisA