长声明的Python可读性

发布于 2025-01-19 10:33:28 字数 935 浏览 7 评论 0原文

我打电话给返回很多元素的函数,尊重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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2025-01-26 10:33:28

一个方便的技巧是使用parens允许您在不必逃脱线路的情况下将作业分开:

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

这使得在分配变量结束列表的何时何时开始且函数调用开始。

我建议,如果仅调用此功能这么复杂,您可能想重新考虑此API并返回某种对象(也许像dataclass一样简单命名为),而不是需要所有这些破坏才能使用的匿名元组。例如:

from dataclasses import dataclass

@dataclass
class Collone:
    dernier_attribut: str
    non_explained_beads: int
    non_explained_beads_recipient: str
    non_explained_beads_donor: str
    score: int
    comments: list[str]
    loci_manquants: tuple[int, int]
    edta: str
    temps: list[int]
    summar: int

def formatageRes(
    ws, 
    wrsep, 
    wdsep, 
    feuille, 
    feuille_corrigee, 
    all_epitopes_considered, 
    nbr_ep_considered, 
    nrows
) -> Collone: ...

以便您可以做:

collone = formatageRes(
     ws, 
     wrsep, 
     wdsep, 
     feuille, 
     feuille_corrigee, 
     all_epitopes_considered, 
     nbr_ep_considered, 
     nrows
)

剩余的调用代码基本相同,但是用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:

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

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 or NamedTuple) rather than an anonymous tuple that requires all this destructuring in order to be usable. E.g.:

from dataclasses import dataclass

@dataclass
class Collone:
    dernier_attribut: str
    non_explained_beads: int
    non_explained_beads_recipient: str
    non_explained_beads_donor: str
    score: int
    comments: list[str]
    loci_manquants: tuple[int, int]
    edta: str
    temps: list[int]
    summar: int

def formatageRes(
    ws, 
    wrsep, 
    wdsep, 
    feuille, 
    feuille_corrigee, 
    all_epitopes_considered, 
    nbr_ep_considered, 
    nrows
) -> Collone: ...

so that you can do:

collone = formatageRes(
     ws, 
     wrsep, 
     wdsep, 
     feuille, 
     feuille_corrigee, 
     all_epitopes_considered, 
     nbr_ep_considered, 
     nrows
)

The remainder of the calling code is then essentially the same, but with collone_varname replaced with collone.varname.

花间憩 2025-01-26 10:33:28

我喜欢什么 black 做(您可以在其 Playground ):

(
    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 like what Black does (you can try it on its playground):

(
    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,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文