I understand that current best practice for line continuation is to use implied continuation inside parenthesis. For example:
a = (1 + 2
+ 3 + 4)
From PEP8 (https://www.python.org/dev/peps/pep-0008/):
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.
I intend to follow this convention going forward, however, my question regards how worried I should be about bugs in existing code that continues lines with a backslash:
a = 1 + 2 \
+ 3 + 4
The python docs (https://docs.python.org/2.7/howto/doanddont.html#using-backslash-to-continue-statements) warn that a stray space at the end of a line after the backslash can make the code "subtly wrong," however, as pointed out in this question (How can using Python backslash line continuation be subtly wrong?), the example given simply results in a SyntaxError being raised which is not a subtle problem as it is easily identified. My question, then, is do there exist cases where continuing a line with a backslash causes something worse than a parsing error? I am interested in examples where a mistake in backslash continuations yields a runtime exception or, worse, code that runs silently but with unintended behavior.
Don't use `\` for line continuation by defaultLong 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.
Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.
"Subtly wrong" is subjective; each has her own tolerance of subtlety.
One often-cited example of possibly harmful line continuation, regardless of \
-separated or implicit, is the continuation of strings in a container structure:
available_resources = [
"color monitor",
"big disk",
"Cray"
"on-line drawing routines",
"mouse",
"keyboard",
"power cables",
]
Do the available resources include a Cray supercomputer, or Crayon-line drawing routines
? (This example is from the book "Expert C Programming", adapted for Python here).
This code block isn't ambiguous, but its visual appearance created by continuation + indentation can be deceptive, and may cause human error in understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With