gpt4 book ai didi

python - 在 Python 中链接方法时换行符的正确样式

转载 作者:IT老高 更新时间:2023-10-28 21:33:16 26 4
gpt4 key购买 nike

我有一些这样的代码。休息应该发生在经期之前还是之后?

# 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 二元运算符而不是在它们之后。因此,格式化代码的首选方式如下:

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

两个相关的段落是来自 Maximum Line Length 的这段。部分:

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.

...和整个Should a line break before or after a binary operator?部分:

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 getscattered across different columns on the screen, and each operator ismoved away from its operand and onto the previous line. Here, the eyehas to do extra work to tell which items are added and which aresubtracted:

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

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

Following the tradition from mathematics usually results in morereadable code:

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

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

请注意,如上面的引用所示,PEP 8 使用给出了关于在哪里绕过运算符的相反建议,下面引用以供后代使用:

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 - 在 Python 中链接方法时换行符的正确样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com