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 16:18:49 -0500 Lines: 44 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 PaDkn2j1vcYJACGoHC4b/weyqJQ4ZvrZzeRDa6MA3xZA== 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; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 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; '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; 'wrote:': 0.16; 'refers': 0.18; '2015': 0.20; '"",': 0.22; 'trying': 0.22; 'code,': 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; 'correct': 0.28; 'extending': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'skip:[ 10': 0.31; 'subject:) ': 0.32; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'replace': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 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; 'itself': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'avoid': 0.61; 'more': 0.63; 'of:': 0.66; 'denis': 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:99818 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] -- Terry Jan Reedy