拆包单值元组的可读约定 [英] Readable convention for unpacking single value tuple

查看:76
本文介绍了拆包单值元组的可读约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关 <有关解压缩单值元组的a href = https://stackoverflow.com/questions/2111759/whats-the-best-practice-for-handling-single-value-tuples-in-python>问题 ,但我想知道在共享和维护代码的可读性方面是否存在首选方法。我发现这些内容涉及长功能链(例如ORM查询)时,会引起同事之间的困惑或误读。

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.

是否存在与 pep8 指南?如果不是,那是最清晰,最易读的方法吗?

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

毕竟显式比隐式要好

这篇关于拆包单值元组的可读约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆