Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99818

Re: Could you explain this rebinding (or some other action) on "nums = nums"?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
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 <mailman.84.1449004772.14615.python-list@python.org> (permalink)
References <94c2e42e-1e5f-40cf-9259-26035e277bf3@googlegroups.com> <mailman.37.1435191416.3674.python-list@python.org> <b48d4b38-a28a-4408-8271-f79e850f453f@googlegroups.com> <mailman.51.1448940760.14615.python-list@python.org> <n3l050$l7a$4@dont-email.me>
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 <python-python-list@m.gmane.org>
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 <n3l050$l7a$4@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99818

Show key headers only | View raw


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 "<stdin>", line 1, in <module>
> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Could you explain this rebinding (or some other action) on "nums = nums"? fl <rxjwg98@gmail.com> - 2015-11-30 18:14 -0800
  Re: Could you explain this rebinding (or some other action) on "nums = nums"? MRAB <python@mrabarnett.plus.com> - 2015-12-01 03:32 +0000
    Re: Could you explain this rebinding (or some other action) on "nums = nums"? Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-01 20:32 +0000
      Re: Could you explain this rebinding (or some other action) on "nums = nums"? Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-01 14:44 -0600
        Re: Could you explain this rebinding (or some other action) on "nums = nums"? Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-01 21:37 +0000
          Re: Could you explain this rebinding (or some other action) on "nums = nums"? Erik <python@lucidity.plus.com> - 2015-12-01 22:34 +0000
          Re: Could you explain this rebinding (or some other action) on "nums = nums"? Erik <python@lucidity.plus.com> - 2015-12-01 22:44 +0000
      Re: Could you explain this rebinding (or some other action) on "nums = nums"? Terry Reedy <tjreedy@udel.edu> - 2015-12-01 16:18 -0500
        Re: Could you explain this rebinding (or some other action) on "nums = nums"? Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-01 21:36 +0000
          Re: Could you explain this rebinding (or some other action) on "nums = nums"? Terry Reedy <tjreedy@udel.edu> - 2015-12-01 17:37 -0500

csiph-web