Python Gotcha's?

Chris Angelico rosuav at gmail.com
Thu Apr 5 14:46:09 EDT 2012


On Fri, Apr 6, 2012 at 4:44 AM, Michael Hrivnak <mhrivnak at hrivnak.org> wrote:
> This is not a gotcha, and it's not surprising.  As John described,
> you're assigning a new value to an index of a tuple, which tuples
> don't support.
>
> a[0] += [3]
>
> is the same as
>
> a[0] = a[0] + [3]
>
> which after evaluation is the same as
>
> a[0] = [1, 3]
>
> You can always modify an item that happens to be in a tuple if the
> item itself is mutable, but you cannot add, remove, or replace items
> in a tuple.

It does make sense, and I've never actually had problems with it
myself. But it's come up on the list and clearly been a cause of
confusion for people, so I thought it worth mentioning. And, as it
turns out, it was the entry already on the list.

ChrisA



More information about the Python-list mailing list