问题
This section of the Python docs describes a subtle error that can happen when using backslash line continuation, if you accidentally add a space after the backslash.
It says, the following would be a syntax error (with a space after the backslash):
if foo.bar()['first'][0] == baz.quux(1, 2)[5:9] and \
calculate_number(10, 20) != forbulate(500, 360):
pass
...but that the next snippet would just be subtly wrong (with a space after the backslash):
value = foo.bar()['first'][0]*baz.quux(1, 2)[5:9] \
+ calculate_number(10, 20)*forbulate(500, 360)
I tried to reproduce this with this:
value = 1 + 2 \
+ 3 + 4
but got this:
File "foo.py", line 1
value = 1 + 2 \
^
SyntaxError: unexpected character after line continuation character
Under what circumstances would that accidentally-added space not be a syntax error?
来源:https://stackoverflow.com/questions/27368510/how-can-using-python-backslash-line-continuation-be-subtly-wrong