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


Groups > comp.lang.python > #99820

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

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Could you explain this rebinding (or some other action) on "nums = nums"?
Date 2015-12-01 21:36 +0000
Organization A noiseless patient Spider
Message-ID <n3l3si$l7a$5@dont-email.me> (permalink)
References (1 earlier) <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>

Show all headers | View raw


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.

After the error, 

>>> tup[1]
[4, 5, 6, 7, 8, 9]
>>> tup
([1, 2, 3], [4, 5, 6, 7, 8, 9])

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.

-- 
Denis McMahon, denismfmcmahon@gmail.com

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