Python Gotcha's?

Chris Angelico rosuav at gmail.com
Wed Apr 4 19:32:44 EDT 2012


On Thu, Apr 5, 2012 at 8:34 AM, Miki Tebeka <miki.tebeka at gmail.com> wrote:
> Greetings,
>
> I'm going to give a "Python Gotcha's" talk at work.
> If you have an interesting/common "Gotcha" (warts/dark corners ...) please share.
>
> (Note that I want over http://wiki.python.org/moin/PythonWarts already).

Don't know if it's what's meant on that page by the += operator, but
the weirdness with concatenating onto a list inside a tuple may merit
a mention.

>>> a=([1],)
>>> a[0].append(2) # This is fine
>>> a
([1, 2],)
>>> a[0]+=[3] # This is not.
Traceback (most recent call last):
  File "<pyshell#134>", line 1, in <module>
    a[0]+=[3]
TypeError: 'tuple' object does not support item assignment
>>> a
([1, 2, 3],)

Throws exception, but still does append to the list.

ChrisA



More information about the Python-list mailing list