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


Groups > comp.lang.python > #99825

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 17:37:34 -0500
Lines 101
Message-ID <mailman.89.1449009497.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> <mailman.84.1449004772.14615.python-list@python.org> <n3l3si$l7a$5@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 u74UOcstX7wmZ+y1FjggAAKemlgYax8wp7W++SHkQ+DQ==
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; '[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 <n3l3si$l7a$5@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:99825

Show key headers only | View raw


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 "<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]
>
> 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 "<pyshell#2>", line 1, in <module>
     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 "<pyshell#7>", line 1, in <module>
     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

Back to comp.lang.python | Previous | NextPrevious 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