Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66667 > unrolled thread
| Started by | John O'Hagan <research@johnohagan.com> |
|---|---|
| First post | 2014-02-19 18:14 +1100 |
| Last post | 2014-02-19 10:54 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | John O'Hagan <research@johnohagan.com> |
|---|---|
| Date | 2014-02-19 18:14 +1100 |
| Subject | Re: a question about list as an element in a tuple |
| Message-ID | <mailman.7137.1392794077.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-02-19 10:51 +0200 |
| Message-ID | <87bny38oz9.fsf@elektro.pacujo.net> |
| In reply to | #66667 |
John O'Hagan <research@johnohagan.com>:
> 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!
Nice catch! The += operator rebinds the reference even if the object
wouldn't change:
>>> t = 1,
>>> t[0] = t[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
See also:
>>> a = []
>>> b = a
>>> a = a + [1]
>>> a is b
False
>>> a = []
>>> b = a
>>> a += [1]
>>> a is b
True
This behavior is not a bug, though. <URL:
http://docs.python.org/3.2/library/operator.html#inplace-operators>:
for example, the statement x += y is equivalent to x =
operator.iadd(x, y)
operator.iadd(x, y) modifies x in place and returns x. However, (<URL:
http://docs.python.org/3.2/library/operator.html#operator.add>) x + y is
dealt with by operator.add(x, y), which leaves x and y intact and must
return a new object.
Marko
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-02-19 10:54 +0200 |
| Message-ID | <877g8r8ots.fsf@elektro.pacujo.net> |
| In reply to | #66677 |
Marko Rauhamaa <marko@pacujo.net>: > operator.add(x, y) [...] leaves x and y intact and must return a new > object. Well, if the addition doesn't modify x, the method can of course return x. Marko
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web