长声明的Python可读性
我打电话给返回很多元素的函数,尊重pep8的方法是执行以下操作,但我认为它不太可读:
colonne_dernier_attribut, colonne_non_explained_beads, colonne_non_explained_beads_recipient, \
colonne_non_explained_beads_donor, colonne_score, colonne_comments, colonne_loci_manquants, \
colonne_edta, colonne_temps, colonne_summary = \
formatageRes(ws, wrsep, wdsep, feuille, feuille_corrigee, all_epitopes_considered, nbr_ep_considered, nrows)
我想要这样的东西,我们可以清楚地看到呼叫的地方到功能。换句话说,我希望在=
符号之前和之后看到明显的差异。
colonne_dernier_attribut, colonne_non_explained_beads, colonne_non_explained_beads_recipient, \
colonne_non_explained_beads_donor, colonne_score, colonne_comments, colonne_loci_manquants, \
colonne_edta, colonne_temps, colonne_summary = \
formatageRes(ws, wrsep, wdsep, feuille, feuille_corrigee, all_epitopes_considered, nbr_ep_considered, nrows)
但这不是遵循PEP8指南。我正在寻找第三个替代方案,它既可以阅读,又遵循PEP8。
I have a call to a function which returns a lot of elements, and the way to respect PEP8 is to do the following, but I don't find it very readable:
colonne_dernier_attribut, colonne_non_explained_beads, colonne_non_explained_beads_recipient, \
colonne_non_explained_beads_donor, colonne_score, colonne_comments, colonne_loci_manquants, \
colonne_edta, colonne_temps, colonne_summary = \
formatageRes(ws, wrsep, wdsep, feuille, feuille_corrigee, all_epitopes_considered, nbr_ep_considered, nrows)
I would want something like this where we can clearly see where the call to the function. In other words, I would like to see a clear difference before and after the =
sign.
colonne_dernier_attribut, colonne_non_explained_beads, colonne_non_explained_beads_recipient, \
colonne_non_explained_beads_donor, colonne_score, colonne_comments, colonne_loci_manquants, \
colonne_edta, colonne_temps, colonne_summary = \
formatageRes(ws, wrsep, wdsep, feuille, feuille_corrigee, all_epitopes_considered, nbr_ep_considered, nrows)
But this isn't following the PEP8 guidelines. I'm looking for a third alternative which is both more readable and follows PEP8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个方便的技巧是使用parens允许您在不必逃脱线路的情况下将作业分开:
这使得在分配变量结束列表的何时何时开始且函数调用开始。
我建议,如果仅调用此功能这么复杂,您可能想重新考虑此API并返回某种对象(也许像
dataclass
或一样简单命名为
),而不是需要所有这些破坏才能使用的匿名元组。例如:以便您可以做:
剩余的调用代码基本相同,但是用
collone_varname
替换为collone.varname
。One handy trick is to use parens to allow you to split the assignments across lines without having to escape the linebreaks:
This makes it pretty obvious where the list of assigned variables ends and the function call begins.
I'd suggest though that if just calling this function is this complicated, you probably want to rethink this API and have it return some kind of object (maybe just something as simple as a
dataclass
orNamedTuple
) rather than an anonymous tuple that requires all this destructuring in order to be usable. E.g.:so that you can do:
The remainder of the calling code is then essentially the same, but with
collone_varname
replaced withcollone.varname
.我喜欢什么 black 做(您可以在其 Playground ):
I like what Black does (you can try it on its playground):