Bin Chen Bin Chen - 2 years ago 131
Python Question

Is it possible to break a long line to multiple lines in Python

Just like C, you can break a long line into multiple short lines. But in Python, if I do this, there will be an indent error... Is it possible?

Answer Source

From 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. The preferred place to break around a binary operator is after the operator, not before it.

Example of implicit line continuation:

a = some_function(
    '1' + '2' + '3' + '4')

Example of explicit line continuation:

a = '1' + '2' + \
    '3' + '4'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download