Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Could you explain this rebinding (or some other action) on "nums = nums"? Date: Tue, 1 Dec 2015 17:37:34 -0500 Lines: 101 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 u74UOcstX7wmZ+y1FjggAAKemlgYax8wp7W++SHkQ+DQ== 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; 'mrab': 0.05; 'tries': 0.05; 'assignment': 0.07; '[1,': 0.09; 'attempted': 0.09; 'augmented': 0.09; 'noted,': 0.09; 'posted.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'typeerror:': 0.09; 'bug': 0.10; 'python': 0.10; 'jan': 0.11; 'exception': 0.13; '2.7.3': 0.16; '3],': 0.16; 'bug:': 0.16; 'mutated': 0.16; 'occurred.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'subject:Could': 0.16; 'tup': 0.16; 'tuple,': 0.16; 'twice.': 0.16; 'wrote:': 0.16; 'refers': 0.18; '>>>': 0.20; '2015': 0.20; '"",': 0.22; 'doc': 0.22; 'info.': 0.22; 'trying': 0.22; 'code,': 0.23; 'bit': 0.23; 'dec': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'not.': 0.27; 'correct': 0.28; '-0500,': 0.29; 'extending': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'skip:[ 10': 0.31; '[1]': 0.32; 'subject:) ': 0.32; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'list': 0.34; 'clear': 0.35; 'replace': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'evaluation': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'list.': 0.37; 'itself': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'ever': 0.60; 'avoid': 0.61; 'above,': 0.63; 'more': 0.63; 'of:': 0.66; 'repeat': 0.67; 'carefully': 0.72; 'denis': 0.84; 'post,': 0.84; 'subject:this': 0.85; 'subject:you': 0.85; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-71-185-227-36.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.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:99825 On 12/1/2015 4:36 PM, Denis McMahon wrote: > On Tue, 01 Dec 2015 16:18:49 -0500, Terry Reedy wrote: > >> On 12/1/2015 3:32 PM, Denis McMahon wrote: >>> On Tue, 01 Dec 2015 03:32:31 +0000, MRAB wrote: >>> >>>> 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. >>> >>> I think I might have found a bug: >> >> What you found is an specific example of what MRAB said in general >> above. >> >>> $ python Python 2.7.3 (default, Jun 22 2015, 19:33:41) >>> [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" >>> for more information. >>>>>> tup = [1,2,3],[4,5,6] >>>>>> tup >>> ([1, 2, 3], [4, 5, 6]) >>>>>> tup[1] >>> [4, 5, 6] >>>>>> tup[1] += [7,8,9] >>> Traceback (most recent call last): >>> File "", line 1, in >>> TypeError: 'tuple' object does not support item assignment >> >> The bug is trying to replace a member of a tuple. The correct code, to >> avoid the exception while extending the list, is >> >> tup[1].extend([7,8,9]) >> >>>>>> tup[1] >>> [4, 5, 6, 7, 8, 9] > > You snipped the important bit of my original post, which was the state of > tup after the TypeError occurred. No I did not. The change to tup[1] right there above, after giving the correct way to make the change. > After the error, >>>> tup[1] > [4, 5, 6, 7, 8, 9] This is exactly what I posted. >>>> tup > ([1, 2, 3], [4, 5, 6, 7, 8, 9]) This is a repeat of the same thing and adds no new info. > The "bug" I refer to is that despite giving the TypeError, the tuple > allowed the assignment of the mutated list to replace the original list. No it did not. As MRAB noted, the list is mutated and the attempted assignment causes an exeption. This has been discussed before more than once ever since augmented *assignment* was introduced. >>> tup = ([],[]) >>> id(tup[1]) 711662188872 >>> tup[1] += [1] Traceback (most recent call last): File "", line 1, in tup[1] += [1] TypeError: 'tuple' object does not support item assignment >>> tup[1] [1] >>> id(tup[1]) 711662188872 >>> tup[1] = tup[1].extend([2]) Traceback (most recent call last): File "", line 1, in tup[1] = tup[1].extend([2]) TypeError: 'tuple' object does not support item assignment >>> tup[1] [1, 2] >>> id(tup[1]) 711662188872 Reading the augmented assignment doc carefully should make it clear that "tup[1] += [1]" is the same as "tup[1] = tup[1].extend([2])" except that evaluation of "tup[1]" happens just once instead of twice. -- Terry Jan Reedy