views:

60

answers:

1

There are some related questions about unpacking single-value tuples, but I'd like to know if there is a preferred method in terms of readability for sharing and maintaining code. I'm finding these to be a source of confusion or misreading among colleagues when they involve a long function chain such as an ORM query.

Is there some convention for this similar to the pep8 guidelines? If not, which is the clearest, most readable way to do it?

Below are the ways I've tried, and my thoughts on them.

Two common methods that are neat but easy to miss:

value, = long().chained().expression().that().returns().tuple()

value = long().chained().expression().that().returns().tuple()[0]

A function would be explicit, but non-standard:

value = unpack_tuple(long().chained().expression().that().returns().tuple())

Maybe always commenting would be the most clear?

# unpack single-value tuple
value, = long().chained().expression().that().returns().tuple()
+9  A: 

How about using explicit parenthesis to indicate that you are unpacking a tuple?

(value, ) = long().chained().expression().that().returns().tuple()

After all explicit is better than implicit.

Manoj Govindan
i like your thinking
pyrony
Nicely explicit
Rod
That's terrific, thank you!
Ian Mackinnon