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


Groups > comp.lang.python > #27126

Re: A difficulty with lists

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: A difficulty with lists
Date Wed, 15 Aug 2012 20:21:22 -0400
References <jvp75j$opr$1@news.albasani.net> <16702a22-6ce3-4120-bbcc-9649e1717130@googlegroups.com> <CAH1RViiN_dBLHbLbC07nQOGZnrPmhahcudk9aJJuB96uyjck5A@mail.gmail.com> <CAH1RVigC9cSkC78jOFU_p9q5zJbTRR3PuL=kiSAYq6ryk9Truw@mail.gmail.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 <CAH1RVigC9cSkC78jOFU_p9q5zJbTRR3PuL=kiSAYq6ryk9Truw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3334.1345076503.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.
>     <http://mail.python.org/mailman/listinfo/python-list>

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

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


Thread

A difficulty with lists Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-06 21:50 +0200
  Re: A difficulty with lists MRAB <python@mrabarnett.plus.com> - 2012-08-06 21:19 +0100
  Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-15 14:12 -0700
    Re: A difficulty with lists Terry Reedy <tjreedy@udel.edu> - 2012-08-15 20:21 -0400
      Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-16 06:46 -0700
  Re: A difficulty with lists Madison May <worldpeaceagentforchange@gmail.com> - 2012-08-15 16:56 -0700
  Re: A difficulty with lists Cheng <chbeh88@googlemail.com> - 2012-08-20 13:43 -0700

csiph-web