Is Line Continuation With Backslash Dangerous In Python?
Solution 1:
"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.
Solution 2:
One case I can think of is when the programmer forgets a \
. This is especially likely when someone comes and adds values at a later date. Though this example is still somewhat contrived because the continuation lines have to have the same indentation level (otherwise it would fail with an IndentationError).
Example:
a = 1 \
+ 2 \+ 3
assert a == 6
Later, someone adds a line:
a = 1 \
+ 2 \
+ 3 # Whoops, forgot to add \ !
+ 4
assert a == 10 # Nope
Solution 3:
I think like @0x5453, if you wrongly add backslash to your code. This backslash cause the comment to be concat with a.
a = "Some text. " \
"""Here are some multiline comments
that will be added to a"""
Post a Comment for "Is Line Continuation With Backslash Dangerous In Python?"