Path: csiph.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'subsequent': 0.04; 'modified': 0.05; 'modify': 0.05; 'assign': 0.07; 'function,': 0.07; 'parameter': 0.07; 'python': 0.09; '*args': 0.09; '*name*': 0.09; '[1,': 0.09; 'function:': 0.09; 'madison': 0.09; 'objects.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sequence,': 0.16; 'useless.': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'input': 0.18; 'code.': 0.20; 'variable': 0.20; 'assignment': 0.22; 'modifying': 0.22; 'object.': 0.22; 'statement': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'right.': 0.27; 'separate': 0.27; 'instead.': 0.27; 'object,': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'url:mailman': 0.29; 'objects': 0.29; 'points': 0.29; "i'm": 0.29; 'becomes': 0.30; 'function': 0.30; 'gets': 0.32; 'url:python': 0.32; 'subject:lists': 0.32; 'url:listinfo': 0.32; 'function.': 0.33; 'point,': 0.33; 'true.': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'that,': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'subject:with': 0.36; 'bad': 0.37; 'two': 0.37; 'why': 0.37; '(for': 0.37; 'quite': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'behind': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'within': 0.64; 'day': 0.73; 'received:fios.verizon.net': 0.84; 'returns.': 0.84; 'scenes': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: A difficulty with lists Date: Wed, 15 Aug 2012 20:21:22 -0400 References: <16702a22-6ce3-4120-bbcc-9649e1717130@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345076503 news.xs4all.nl 6957 [2001:888:2000:d::a6]:34377 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27126 On 8/15/2012 5:58 PM, Rob Day wrote: > Madison May wrote: > The list nlist inside of function xx is not the same as the variable > u outside of the function: nlist and u refer to two separate list > objects. When you modify nlist, you are not modifying u. > This is confused and wrong. The parameter *name* 'nlist' of function xx is not the same as the *name* 'u' outside the function. The call xx(u) binds nlist to the same object that u is bound to. At that point, the two name *are* bound to the same list object. The statement "nlist+=[999]" dodifying nlist *does* modify u. The subsequent assignment statement "nlist=nlist[:-1]" rebinds 'nlist' to a *new* list object. That new object gets deleted when the function returns. So the rebinding is completely useless. This sequence, modifying the input argument and then rebinding to a new object, is bad code. > Well - that's not quite true. Before calling the function, u is [1, 2, > 3, 4] - but after calling the function, u is [1, 2, 3, 4, 999]. This is > a result of using 'nlist += [999]' - the same thing doesn't happen if > you use 'nlist = nlist+[999]' instead. > > I'm not completely aware of what's going on behind the scenes here, but you got it right. > I think the problem is that 'nlist' is actually a reference to a list > object - it points to the same place as u. Calling a python function binds parameter names to argument objects or (for *args and **kwds parameters) a collection based on argument objects. > When you assign to it within > the function, then it becomes separate from u - which is why nlist = > nlist+[999] and nlist = nlist[:-1] don't modify u - but if you modify > nlist in place before doing that, such as by using +=, then it's still > pointing to u, and so u gets modified as well. -- Terry Jan Reedy