Today I was improving some code switching (inmutable) lists to tuples, after the change I run the tests, one fails and triying to find the error I discover that the tuples are not really the same than an inmutable list, at least if they have one element. The example:


>>> t = ('ES')
>>> for i in t:
... print i
...
E (this is one element)
S (this is another element but I were expected both together, 'ES')

And:

>>> t = ('ES',) (Notice the comma)
>>> for i in t:
... print i
...
ES (one element, what I expected)

I don’t know if it’s a bug…, in lists is the same if you write or not the comma if only has one element.

Probably I had not discovered it until my app breaks, so remember, on tuples and lists with one element EVER use the comma too, and of course, TDD.

Advertisements