我该如何换行(换行)?
时间:2020-03-05 18:50:26 来源:igfitidea点击:
我有一长行代码,我想在多行中分解。我使用什么,语法是什么?
例如,添加一串字符串,
e = 'a' + 'b' + 'c' + 'd'
并分成两行,如下所示:
e = 'a' + 'b' + 'c' + 'd'
解决方案
回答
在行的末尾添加一个" ",或者将该语句括在括号中的(..)中。从IBM:
b = ((i1 < 20) and (i2 < 30) and (i3 < 40))
或者
b = (i1 < 20) and \ (i2 < 30) and \ (i3 < 40)
回答
什么线?我们只需在下一行就有参数就不会有任何问题:
a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, blahblah6, blahblah7)
否则,我们可以执行以下操作:
if a == True and \ b == False
查看样式指南以获取更多信息。
从示例行中:
a = '1' + '2' + '3' + \ '4' + '5'
或者:
a = ('1' + '2' + '3' + '4' + '5')
请注意,样式指南指出,最好使用带括号的隐式连续,但是在这种特殊情况下,仅在表达式周围加上括号可能是错误的方法。
回答
From the horse's mouth: Explicit line joining Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example: if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1 A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.
回答
从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. Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable: with open('/path/to/some/file/you/want/to/read') as file_1, \ open('/path/to/some/file/being/written', 'w') as file_2: file_2.write(file_1.read()) Another such case is with assert statements. 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)
编辑:PEP8现在建议数学家及其发布者使用相反的约定(用于在二进制操作处中断)以提高可读性。
唐纳德·克努斯(Donald Knuth)在二进制运算符垂直对齐之前先断后合的风格,从而在确定要添加和减去的项时减少了工作量。
在PEP8中:换行符应该在二进制运算符之前还是之后?
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"[3]. 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) 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.
[3]:Donald Knuth的The TeXBook,第195和196页
回答
使用反斜杠结束行的危险在于,如果在反斜杠之后添加空格(当然很难看到),则反斜杠不再像我们想象的那样工作。
有关更多信息,请参见Python惯用语和反惯用语(适用于Python 2或者Python 3)。
回答
我们可以在括号和花括号之间打断线。另外,我们可以将反斜杠字符" "添加到一行以显式地将其断开:
x = (tuples_first_value, second_value) y = 1 + \ 2