Unpacking and Conditional Expressions

Unpacking tuples and conditional expressions in Python.

The other day whilst working on a web crawler at work called spyda I came across an interesting behavior in Python. Here it goes:

>>> xs = [(1, 2), (3, 4)]
>>> a, b = xs[0] if xs else None, None
>>> a
(1, 2)
>>> b

Notice that a becomes the first tuple in the list and b becomes None? This kinda got me stumped at first until I realized what was happening with the Python parser. If however you create your expression like this:

>>> a, b = xs[0] if xs else (None, None)
>>> a
>>> b

Then everything is all good!

The lesson here (I think) is to use parenthesis here to both make things clearer to the reader and to ensure the correct expression evaluation.