我有一个很长的“和”声明,例如:
if this == that and this == that and this == that:
do this
如何正确地将此类语句分成单独的行,以符合 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.
和示例:
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))
关于python - 如何缩短长 "and"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830793/