在Python中链接方法时,换行的样式正确 [英] Correct style for line breaks when chaining methods in Python

查看:168
本文介绍了在Python中链接方法时,换行的样式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的代码.休息时间应该发生在时段之前还是之后?

I have some code like this. Should the break occur before the periods or after?

# before
my_var = somethinglikethis.where(we=do_things).where(we=domore).where(we=everdomore)

# this way
my_var = somethinglikethis.where(we=do_things) \
                          .where(we=domore) \
                          .where(we=everdomore)

# or this way
my_var = somethinglikethis.where(we=do_things). \
                           where(we=domore). \
                           where(we=everdomore)

推荐答案

PEP 8建议使用括号,这样您就不需要\,并轻轻地建议在 before 之前而不是after之后使用二进制运算符他们.因此,格式化代码的首选方式是这样的:

PEP 8 recommends using parenthesis so that you don't need \, and gently suggests breaking before binary operators instead of after them. Thus, the preferred way of formatting you code is like this:

my_var = (somethinglikethis
          .where(we=do_things)
          .where(we=domore)
          .where(we=everdomore))

这两个相关的段落来自最大行长度部分:

The two relevant passages are this one from the Maximum Line Length section:

包裹长行的首选方法是在括号,方括号和花括号内使用Python的隐含行连续性.通过将表达式包装在括号中,可以将长行分成多行.应该优先使用这些字符,而不要使用反斜杠来继续行行.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

...以及整个

... and the entire Should a line break before or after a binary operator? section:

在二进制运算符之前或之后应该换行吗?

几十年来,推荐的样式是在二元运算符之后使用. 但这会从两个方面损害可读性:操作员倾向于 分散在屏幕上的不同列中,每个运算符是 从其操作数移至上一行.在这里,眼睛 必须做额外的工作才能知道添加了哪些项目,哪些是 减去:

Should a line break before or after a binary operator?

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line. Here, the eye has to do extra work to tell which items are added and which are subtracted:

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

为了解决此可读性问题,数学家及其出版商 遵循相反的约定.唐纳德·克努斯(Donald Knuth)解释了传统 他的计算机与排版系列中的一条规则:尽管公式 段落中的内容总是在二进制运算和关系之后中断, 显示的公式总是在二进制运算之前中断"

To solve this readability problem, mathematicians and their publishers follow the opposite convention. Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"

遵循数学的传统通常会带来更多 可读代码:

Following the tradition from mathematics usually results in more readable code:

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

在Python代码中,允许在二进制文件之前或之后中断 运算符,只要约定在本地是一致的.对于新 建议使用Knuth的样式.

In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.

请注意,如上面引文所述,PEP 8用于给出关于在何处中断运算符的相反建议,以下引用为后人引言:

Note that, as indicated in the quote above, PEP 8 used to give the opposite advice about where to break around an operator, quoted below for posterity:

包裹长行的首选方法是使用Python的隐含行 括号,方括号和大括号内的延续.长线可以 通过将表达式包装在括号中,将多行分开.这些 应优先使用反斜线进行行连续. 确保适当缩进续行.首选地点 绕开二元运算符是在运算符之后,而不是在运算符之前. 一些例子:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        if (width == 0 and height == 0 and
            color == 'red' and emphasis == 'strong' or
            highlight > 100):
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)

这篇关于在Python中链接方法时,换行的样式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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