就像 C 一样,您可以将一条长线分成多条短线。但是在 Python ,如果我这样做,会出现缩进错误...有可能吗?
最佳答案
来自 PEP 8 - Style Guide for Python Code :
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.
隐式续行示例:
a = some_function(
'1' + '2' + '3' - '4')
关于二元运算符周围的换行符,它继续说:
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.
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 (line breaks before the operator) is suggested.
显式续行示例:
a = '1' \
+ '2' \
+ '3' \
- '4'
关于python - 是否可以在 Python 中将长行分成多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4172448/