python - 是否可以在 Python 中将长行分成多行?

标签 python line-breaks

就像 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/

相关文章:

python - Pandas 在数据帧上合并,同时保持相同的行数

javascript - 如何在 JavaScript 中解码 JSON?

PHP:如何使用带有 HTML 净化器的 nl2br() 来保持换行符?

python - 编写指定行号和列号的 Excel 文件 - openpyxl

python - 如何在 Django 中获取文件的扩展名?

html - 跨多行包装标记时如何避免空格

Ruby 或正则表达式去除多余的换行符

powershell - 使用外档时避免换行

android - 如何在 Android 的 xml <string> 资源中保留换行符?

python - 我怎样才能找到图片中突出显示的元素。我用 Selenium