Readable convention for unpacking single value tuple

余生长醉 提交于 2019-12-05 21:13:30

问题


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()

回答1:


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.



来源:https://stackoverflow.com/questions/3721477/readable-convention-for-unpacking-single-value-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!