如何在 Python 三元运算符上换行?
How to make a line break on the Python ternary operator?
有时 Python 中包含三元运算符的行变得太长:
answer = 'Ten for that? You must be mad!' if does_not_haggle(brian) else "It's worth ten if it's worth a shekel."
是否有推荐的使用三元运算符在 79 个字符处换行的方法?我没有在 PEP 8 中找到它。
您始终可以使用括号扩展 logical line across multiple physical lines:
answer = (
'Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
上面使用了 PEP8 一切都缩进一步的风格(称为 hanging indent)。您还可以缩进额外的行以匹配左括号:
answer = ('Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
但这会让您更快达到 80 列最大值。
if
和 else
部分的具体位置由您决定;我在上面使用了我个人的喜好,但是还没有任何人都同意的运算符的特定样式。
PEP8 说 preferred way of breaking long lines is using parentheses:
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.
answer = ('Ten for that? You must be mad!'
if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
牢记来自 Python 的禅宗的建议:
"Readability counts."
三元运算符全部在一行时可读性最好。
x = y if z else w
当您的条件或变量将行推到超过 79 个字符(参见 PEP8)时,可读性开始受到影响。 (可读性也是 dict/list 理解最好保持简短的原因。)
因此,与其尝试使用括号来换行,不如将其转换为常规 if
块,您可能会发现它更具可读性。
if does_not_haggle(brian):
answer = 'Ten for that? You must be mad!'
else:
answer = "It's worth ten if it's worth a shekel."
奖励:上述重构揭示了另一个可读性问题:does_not_haggle
是倒逻辑。如果您可以重写函数,这将更具可读性:
if haggles(brian):
answer = "It's worth ten if it's worth a shekel."
else:
answer = 'Ten for that? You must be mad!'
有时 Python 中包含三元运算符的行变得太长:
answer = 'Ten for that? You must be mad!' if does_not_haggle(brian) else "It's worth ten if it's worth a shekel."
是否有推荐的使用三元运算符在 79 个字符处换行的方法?我没有在 PEP 8 中找到它。
您始终可以使用括号扩展 logical line across multiple physical lines:
answer = (
'Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
上面使用了 PEP8 一切都缩进一步的风格(称为 hanging indent)。您还可以缩进额外的行以匹配左括号:
answer = ('Ten for that? You must be mad!' if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
但这会让您更快达到 80 列最大值。
if
和 else
部分的具体位置由您决定;我在上面使用了我个人的喜好,但是还没有任何人都同意的运算符的特定样式。
PEP8 说 preferred way of breaking long lines is using parentheses:
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.
answer = ('Ten for that? You must be mad!'
if does_not_haggle(brian)
else "It's worth ten if it's worth a shekel.")
牢记来自 Python 的禅宗的建议: "Readability counts."
三元运算符全部在一行时可读性最好。
x = y if z else w
当您的条件或变量将行推到超过 79 个字符(参见 PEP8)时,可读性开始受到影响。 (可读性也是 dict/list 理解最好保持简短的原因。)
因此,与其尝试使用括号来换行,不如将其转换为常规 if
块,您可能会发现它更具可读性。
if does_not_haggle(brian):
answer = 'Ten for that? You must be mad!'
else:
answer = "It's worth ten if it's worth a shekel."
奖励:上述重构揭示了另一个可读性问题:does_not_haggle
是倒逻辑。如果您可以重写函数,这将更具可读性:
if haggles(brian):
answer = "It's worth ten if it's worth a shekel."
else:
answer = 'Ten for that? You must be mad!'