Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Could you explain this rebinding (or some other action) on "nums = nums"? Date: Tue, 1 Dec 2015 03:32:31 +0000 Lines: 118 Message-ID: References: <94c2e42e-1e5f-40cf-9259-26035e277bf3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de qmTzCtXh6NUNb9Q36PZZPAf100cXQEDi/4L0HDl06lZg== 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; 'tries': 0.05; 'assignment': 0.07; 'implements': 0.07; 'method,': 0.07; 'msg': 0.07; 'objects,': 0.07; 'wednesday,': 0.07; '[1,': 0.09; 'mutable': 0.09; 'question?': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'itself.': 0.11; 'def': 0.13; 'thu,': 0.15; '*always*': 0.16; '24,': 0.16; '3],': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'in-place': 0.16; 'instead:': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'no-op,': 0.16; 'operation,': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Could': 0.16; 'test()': 0.16; 'tup': 0.16; 'tuple,': 0.16; 'which,': 0.16; 'wrote:': 0.16; 'string': 0.17; 'refers': 0.18; '>>>': 0.20; 'changes': 0.20; '2015': 0.20; 'first,': 0.20; '"",': 0.22; 'object.': 0.22; 'trying': 0.22; 'am,': 0.23; 'code,': 0.23; 'this:': 0.23; 'thanks,': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'chris': 0.26; 'error': 0.27; 'necessary,': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'that.': 0.30; 'code': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'received:84': 0.32; 'subject:) ': 0.32; 'getting': 0.33; 'class': 0.33; 'instead,': 0.33; 'traceback': 0.33; 'file': 0.34; 'list': 0.34; 'list:': 0.35; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'itself': 0.38; 'self': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; "didn't": 0.39; 'received:192': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'skip:n 10': 0.62; 'different': 0.63; 'complete': 0.63; 'of:': 0.66; 'here': 0.66; 'acts': 0.72; 'as:': 0.79; "'foo'": 0.84; 'chrisa': 0.84; 'utc-4,': 0.84; 'world!"': 0.84; 'subject:this': 0.85; 'subject:you': 0.85; 'do:': 0.91; 'write:': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=KRgAG9gnAAAA:8 a=D6_5wkXDe-9tF-dZ05kA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: 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: , Xref: csiph.com comp.lang.python:99765 On 2015-12-01 02:14, fl wrote: > On Wednesday, June 24, 2015 at 8:17:08 PM UTC-4, Chris Angelico wrote: >> 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 > > I have revisit the past post. In the example code snippet: > > type(tup[1]) > Out[162]: list > > 'list' is mutable. Why does the following line have errors? > In practical Python code, error is not acceptable. Then, what purpose is > for the following code here to show? > > Thanks, > > >>>> tup[1] += [6,7] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment > When you write: x += y Python tries to do: x = x.__iadd__(y) If x doesn't have the "__iadd__" method, Python then tries to do: x = x.__add__(y) The "__iadd__" method should mutate the object in-place and then return itself. Here's an example where it returns something else instead: # Example class Test: def __init__(self): self.string = '' def __iadd__(self, other): self.string += other return "Surprise!" t = Test() t += 'foo' print(t) # End of example In the case of: tup[1] += [6, 7] what it's trying to do is: tup[1] = tup[1].__iadd__([6, 7]) tup[1] refers to a list, and the __iadd__ method _does_ mutate it, but then Python tries to put the result that the method returns into tup[1]. That fails because tup itself is a tuple, which is immutable.