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


Groups > comp.lang.python > #66667

Re: a question about list as an element in a tuple

Date 2014-02-19 18:14 +1100
From John O'Hagan <research@johnohagan.com>
Subject Re: a question about list as an element in a tuple
References <CAFysF+SirN1VKMhCcuWjLXkK--+V2Bxs4AbntJgTjcMDMhrWuA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7137.1392794077.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, 16 Dec 2013 11:30:13 +0800
liuerfire Wang <liuerfire@gmail.com> wrote:

> Just like below:
> 
> In [1]: a = ([], [])
> 
> In [2]: a[0].append(1)
> 
> In [3]: a
> Out[3]: ([1], [])
> 
> In [4]: a[0] += [1]
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last) <ipython-input-4-ea29ca190a4d> in <module>()
> ----> 1 a[0] += [1]
> 
> TypeError: 'tuple' object does not support item assignment
> 
> In [5]: a
> Out[5]: ([1, 1], [])
> 
> no problem, there is an exception. But a is still changed.
> 
> is this a bug, or could anyone explain it?
> 
> thanks.
> 

This thread from two years ago deals with this in some detail:

https://mail.python.org/pipermail/python-list/2012-February/619265.html

It's one of those things that is hard to resolve in a way that makes
sense in every situation. The weirdest part for me is this:

>>> t = ([],)
>>> l = t[0]
>>> l is t[0]
True
>>> l += [1]
>>> t[0] += [1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Whether there is an error or not depends on the name used for the
object!

--

John

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


Thread

Re: a question about list as an element in a tuple John O'Hagan <research@johnohagan.com> - 2014-02-19 18:14 +1100
  Re: a question about list as an element in a tuple Marko Rauhamaa <marko@pacujo.net> - 2014-02-19 10:51 +0200
    Re: a question about list as an element in a tuple Marko Rauhamaa <marko@pacujo.net> - 2014-02-19 10:54 +0200

csiph-web