Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'explicitly': 0.05; 'assignment': 0.07; 'modified': 0.07; 'modify': 0.07; 'modifying': 0.07; 'referring': 0.07; '[0,': 0.09; 'immutable': 0.09; 'objects,': 0.09; 'illustrates': 0.16; 'meanwhile,': 0.16; 'objects.': 0.16; 'subsequently': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'feb': 0.22; 'appears': 0.22; 'example': 0.22; 'affects': 0.24; 'regardless': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; '>>>>': 0.31; 'reflected': 0.31; 'lists': 0.32; '(e.g.': 0.33; 'fri,': 0.33; 'not.': 0.33; 'case,': 0.35; 'received:google.com': 0.35; 'there': 0.35; '14,': 0.36; 'object,': 0.36; 'thanks': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'short': 0.38; 'explain': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'dave': 0.60; 'affect': 0.61; 'first': 0.61; 'name': 0.63; 'refer': 0.63; 'spam.': 0.64; 'different': 0.65; 'between': 0.67; 'response.': 0.68; 'other.': 0.75; '100': 0.79; '"spam"': 0.84; 'all;': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hK+PQhkoNutFVZz39zKJ4Z3lv62bEddKZCC6HIxFiVI=; b=YUX5jLi7r6wNsqfAQ7r3z/0Ra9jpjkEjXVnPzvfHQh5zvRZIfL9EHW4lxHtVP0ALig 6F2hF+XSCcNlvQpH0DfO83nn+9usSvx+2xLqi4wGyOXlx7jbVlyvZnjiZNDC5RgpSB29 Q+izRO/C4h7swQljQJFS2SKFZWm9lbT8xsEo9wTc1d+eXrHNSy5GKLTj4FLsm6yBDrA5 qVkp0WByoL2KF81OjHbnk61aSfWDRdbbQykYY5tG0NdKljokBjaYVj0Z8RUe2duBSyvQ SE2LHnZksFt7rsrnjQ6ZqhPcLzPUHz/gFQDVzFtE/G+mZg0PwBymdNmrKvhxzhjhdOdX Q91g== X-Received: by 10.68.75.9 with SMTP id y9mr11302178pbv.61.1392408811767; Fri, 14 Feb 2014 12:13:31 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <917ede6d-db7c-4a8c-8203-27677283776b@googlegroups.com> References: <13208de8-0f85-4e60-b059-dc087c8fda41@googlegroups.com> <917ede6d-db7c-4a8c-8203-27677283776b@googlegroups.com> From: Ian Kelly Date: Fri, 14 Feb 2014 13:12:51 -0700 Subject: Re: Explanation of list reference To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392408815 news.xs4all.nl 2833 [2001:888:2000:d::a6]:52108 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66321 On Fri, Feb 14, 2014 at 11:54 AM, dave em wrote: > Thanks for your quick response. I'm still not sure we understand. The code below illustrates the concept we are trying to understand. > > Case 1: Example of variable with a specific value from P 170 of IYOCGWP > >>>> spam = 42 >>>> cheese = spam >>>> spam = 100 >>>> spam > 100 >>>> cheese > 42 > > Case 2: Example of variable with a list reference from p 170 > >>>> spam = [0, 1, 2, 3, 4, 5] >>>> cheese = spam >>>> cheese[1] = 'Hello!' >>>> spam > [0, 'Hello!', 2, 3, 4, 5] >>>> cheese > [0, 'Hello!', 2, 3, 4, 5] > > What I am trying to explain is this, why in case 1 when acting on spam (changing the value from 42 to 100) only affects spam and not cheese. Meanwhile, in case two acting on cheese also affects spam. In the first case, after the assignment "cheese = spam", the names spam and cheese are bound to the same object (42). If you were to modify the object 42 (which you cannot do in this case, because ints are immutable) then you would see the change reflected in the object regardless of which name you used to access it. You then rebind the name "spam" to a different object (100), which does not affect the binding of the name "cheese" at all; the names end up referring to different objects. In the second case, after the assignment "cheese = spam", the names again are bound to the same object, a list. The assignment "cheese[1] = 'Hello!'" then *modifies* that list, without rebinding cheese. cheese and spam continue to refer to the same object, and since it was modified you can see the change in that object regardless of which name you used to access it. If in the second case, you were to explicitly copy the list (e.g. "cheese = list(spam)") prior to modifying it, then the two names would instead be bound to different objects, and so subsequently modifying one would not affect the other. So the short answer is that there is no difference at all between the way that names are bound to ints and the way they are bound to lists. There only superficially appears to be a difference because ints are immutable and lists are not.