Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'string.': 0.04; 'modified': 0.05; 'subject:question': 0.08; 'python': 0.09; '[1,': 0.09; 'internally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'anyway': 0.11; 'itself.': 0.11; 'advance.': 0.15; 'modification': 0.15; '"+="': 0.16; '999': 0.16; 'altered': 0.16; 'creation.': 0.16; 'hypothetical': 0.16; 'message-id:@dough.gmane.org': 0.16; 'other:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'lista': 0.17; '>>>': 0.18; 'subject:] ': 0.19; 'variable': 0.20; 'earlier': 0.21; '"",': 0.22; 'assignment': 0.22; 'header:User-Agent:1': 0.26; '(most': 0.27; 'implemented': 0.27; 'question': 0.27; 'list:': 0.27; 'translated': 0.27; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'initial': 0.28; 'now?': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'file': 0.32; '(2)': 0.32; 'subject:lists': 0.32; 'print': 0.32; 'function.': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'knowledge': 0.33; "can't": 0.34; 'skip:- 50': 0.34; '(1)': 0.34; 'self': 0.34; 'thanks': 0.34; 'list': 0.35; 'similar': 0.35; 'received:org': 0.36; 'created': 0.36; 'should': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'item': 0.37; 'perform': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'first': 0.61; "you'll": 0.62; 'manner': 0.74; 'hand': 0.82; 'armed': 0.91; 'str.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: [newbie] A question about lists and strings Date: Fri, 10 Aug 2012 11:59:43 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b240.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 98 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344592775 news.xs4all.nl 6879 [2001:888:2000:d::a6]:36097 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26852 Mok-Kong Shen wrote: > > In an earlier question about lists, I was told about the issue of > creation of local names in a function. However, I still can't > understand why the program below outputs: > > [999] sss > [999] > > and not two identical lines of output. For both operators "+=" should > anyway work in similar manner in the function xx in my view. > > Thanks for your help in advance. > > M. K. Shen > > ---------------------------------------------------------- > > def xx(list,str): > list+=[999] > str+="sss" a += b is internally translated into to a = a.__iadd__(b) If the 'list' class were implemented in Python it would look like class list: def __iadd__(self, other): for item in other: self.append(item) return self i. e. the original list is modified when you perform += and you'll see the modification when you look at any name bound to that original list: b = a = [1, 2] a += [3, 4] print a # [1, 2, 3, 4] print b # [1, 2, 3, 4] Strings on the other hand are "immutable" -- they cannot be altered after the initial creation. The hypothetical __iadd__() implementation is class str: def __iadd__(self, other): return self + other So += rebinds a name to a new string: b = a = "first" a += "second" print b # first print a # firstsecond Armed with this knowledge > lista=[] > stra="" > lista+=[999] [999] is appended to lista and lista is rebound to itself. > stra+="sss" A new string "" + "sss" is created and stra is bound to that new string. > print(lista,stra) > listb=[] > strb="" > xx(listb,strb) Inside xx() (1) 999 is appended to listb and the local variable list is rebound. (2) A new string "" + "sss" is created and bound to the local variable str. > print(listb,strb) If you have understood the above here's a little brain teaser: >>> a = ([1,2,3],) >>> a[0] += [4, 5] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> a[0] What are the contents of a[0] now?