How to extend a tuple of tuples?
Thomas 'PointedEars' Lahn
PointedEars at web.de
Tue Sep 13 17:08:11 EDT 2016
John Gordon wrote:
> […] Thomas 'PointedEars' Lahn […] writes:
It is supposed to be an attribution *line*, _not_ an attribution novel.
>> >> The obvious way does not work -
>> >>
>> >> a += (5, 6)
>> ^^^^^^
>> > Right, because a tuple is immutable.
>
>> How did you get that idea? It has been mutated in the very statement
>> that you are quoting
>
> No. An entirely new tuple is created, and 'a' is rebound to it. The
> existing tuple is not mutated.
Indeed,
| $ python3
| Python 3.4.4 (default, Apr 17 2016, 16:02:33)
| [GCC 5.3.1 20160409] on linux
| Type "help", "copyright", "credits" or "license" for more information.
| >>> t = ((1, 2), (3, 4))
| >>> t.__repr__
| <method-wrapper '__repr__' of tuple object at 0x7f4b2ddd2c08>
| >>> t += (5, 6),
| >>> t.__repr__
| <method-wrapper '__repr__' of tuple object at 0x7f4b2ddca750>
indicates that this is so. However, this argument is purely academic, as
that rebinding happens would neither validate Ben’s argument nor invalidate
mine: The result obtained without the trailing comma is not so “because a
tuple is immutable”, but because of how the expression RHS is parsed.
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
More information about the Python-list
mailing list