开发者

Readable convention for unpacking single value tuple

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 maintain开发者_运维知识库ing 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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜