The importance of the comma (and TDD)

April 7, 2010

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

5 Responses to “The importance of the comma (and TDD)”

  1. Carlos Says:

    I believe it is not a bug. There is some part in our code where we have documented something about that. I think it is in the callbacks module. Take a look at it 🙂

    Thanks to TDD we have a good design and a great test battery 🙂

    Keep on writing this blog mate!

  2. UNIX4ALL Says:

    Oh my god, commas are devil guys my friend.

    :D.

  3. oscarmm Says:

    Ahmsss… wow and you read it… it’s a little outdated 😀

    Cheers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: