Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'operator': 0.03; '(except': 0.05; 'assignment': 0.07; 'implements': 0.07; 'msg': 0.07; 'objects,': 0.07; '[1,': 0.09; 'mutable': 0.09; 'question?': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.14; 'thu,': 0.15; '*always*': 0.16; '3],': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'no- op,': 0.16; 'operation,': 0.16; 'subject:Could': 0.16; 'tup': 0.16; 'which,': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'changes': 0.20; 'first,': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '"",': 0.22; 'object.': 0.22; 'am,': 0.23; '2015': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'message-id:@mail.gmail.com': 0.28; 'necessary,': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'that.': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'subject:) ': 0.32; 'getting': 0.33; 'class': 0.33; 'instead,': 0.33; 'traceback': 0.33; 'file': 0.34; 'received:google.com': 0.34; 'list:': 0.35; 'something': 0.35; 'list': 0.35; 'but': 0.36; 'there': 0.36; 'two': 0.37; "didn't": 0.37; 'subject:: ': 0.37; 'item': 0.38; 'self': 0.38; 'does': 0.39; 'where': 0.40; 'skip:t 20': 0.40; 'subject: (': 0.40; 'simple': 0.61; 'skip:n 10': 0.63; 'complete': 0.63; 'different': 0.64; 'acts': 0.72; 'as:': 0.79; 'chrisa': 0.84; 'world!"': 0.84; 'subject:this': 0.85; 'subject:you': 0.88; 'to:none': 0.90 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=44TSDJa9AqPfkwgy9C45RH8VAXeTvzYH90Noy9BKsnI=; b=h0qWaygwBty5370XWOKVsOyjR6bxCw6FJMqsZ/qQxjt9BW2FQs4YkkFKLrNcPyNcTv SAHyUU0KCxjKvHPI2o5xh1suT44Xdo/fjtiyju42bPFN9JDRjjg1XIb7SVpItb3lnMAe OUMjSFLsWDf01xswXS9Vuxkrtvuh8aMDNVdq2NwnvP5md+KXztXxV3bttOwORAGcxJUs T8chShdm5WCAhHxWBg6J9gPur0GU8sOKq0p1ceF1jeaO+1bzBL37KCClwtS6D6dRa2Uc fOPVEnS1QKF9Gry9j5ZMZwxcSgJdz60sZvrXlaDWz6SjDCUJJikuPVzkUM2bCLQDOVsv h0RQ== MIME-Version: 1.0 X-Received: by 10.43.0.67 with SMTP id nl3mr39760744icb.59.1435191407242; Wed, 24 Jun 2015 17:16:47 -0700 (PDT) In-Reply-To: <94c2e42e-1e5f-40cf-9259-26035e277bf3@googlegroups.com> References: <94c2e42e-1e5f-40cf-9259-26035e277bf3@googlegroups.com> Date: Thu, 25 Jun 2015 10:16:47 +1000 Subject: Re: Could you explain this rebinding (or some other action) on "nums = nums"? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435191416 news.xs4all.nl 2877 [2001:888:2000:d::a6]:34112 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93109 On Thu, Jun 25, 2015 at 9:52 AM, fl wrote: > The reason is that list implements __iadd__ like this (except in C, not Python): > > class List: > def __iadd__(self, other): > self.extend(other) > return self > When you execute "nums += more", you're getting the same effect as: > > nums = nums.__iadd__(more) > which, because of the implementation of __iadd__, acts like this: > > nums.extend(more) > nums = nums > So there is a rebinding operation here, but first, there's a mutating operation, and the rebinding operation is a no-op. It's not a complete no-op, as can be demonstrated if you use something other than a simple name: >>> tup = ("spam", [1, 2, 3], "ham") >>> tup[1] [1, 2, 3] >>> tup[1].extend([4,5]) >>> tup[1] = tup[1] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> tup ('spam', [1, 2, 3, 4, 5], 'ham') >>> tup[1] += [6,7] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> tup ('spam', [1, 2, 3, 4, 5, 6, 7], 'ham') The reason for the rebinding is that += can do two completely different things: with mutable objects, like lists, it changes them in place, but with immutables, it returns a new one: >>> msg = "Hello" >>> msg += ", world!" >>> msg 'Hello, world!' This didn't change the string "Hello", because you can't do that. Instead, it rebound msg to "Hello, world!". For consistency, the += operator will *always* rebind, but in situations where that's not necessary, it rebinds to the exact same object. Does that answer the question? ChrisA