How can using Python backslash line continuation be subtly wrong?

六月ゝ 毕业季﹏ 提交于 2019-12-23 01:43:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!