Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27126
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: A difficulty with lists |
| Date | 2012-08-15 20:21 -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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3334.1345076503.4697.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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