IPython 原始字符串的续行行为不一致
IPython inconsistent behavior in line continuation of raw string
CPython 和 IPython shell 似乎对原始字符串的行延续有着微妙的不同解释。这些行为很容易演示。
- 示例 1:原始字符串的续行。
CPython 结果:
>>> r'abc\
... def'
'abc\\ndef'
IPython shell 结果:
In [1]: r'abc\
...: def'
Out[1]: 'abcdef'
- 示例 2:原始字符串的转义行继续。
CPython 结果(在 \
之后立即按 <Enter>
):
>>> r'abc\
File "<stdin>", line 1
r'abc\
^
SyntaxError: EOL while scanning string literal
IPython shell 结果:
In [1]: r'abc\
...: def'
Out[1]: 'abc\def'
应该清楚,CPython 结果在这两种情况下都是正确的,因为它符合 Python Language Reference:
的部分
[...] Note also that a single backslash followed by a newline is
interpreted as those two characters as part of the string, not as a
line continuation.
以及下一段:
[...] Backslashes can be escaped with a preceding backslash; however, both
remain in the string.
这是 IPython shell 中的错误吗?
相关系统信息:IPython 2.4.1,在 Fedora 22 上使用 CPython 2.7.10 和 3.4.2 进行测试。
问题已在 IPython
github
上提出
https://github.com/ipython/ipython/issues/5828
它被认为是一个错误,但似乎没有很高的优先级。它只影响原始字符串。
我想不出它会困扰我的情况。我在哪里需要 'abc\\ndef'
?
CPython 和 IPython shell 似乎对原始字符串的行延续有着微妙的不同解释。这些行为很容易演示。
- 示例 1:原始字符串的续行。
CPython 结果:
>>> r'abc\
... def'
'abc\\ndef'
IPython shell 结果:
In [1]: r'abc\
...: def'
Out[1]: 'abcdef'
- 示例 2:原始字符串的转义行继续。
CPython 结果(在 \
之后立即按 <Enter>
):
>>> r'abc\
File "<stdin>", line 1
r'abc\
^
SyntaxError: EOL while scanning string literal
IPython shell 结果:
In [1]: r'abc\
...: def'
Out[1]: 'abc\def'
应该清楚,CPython 结果在这两种情况下都是正确的,因为它符合 Python Language Reference:
的部分[...] Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
以及下一段:
[...] Backslashes can be escaped with a preceding backslash; however, both remain in the string.
这是 IPython shell 中的错误吗?
相关系统信息:IPython 2.4.1,在 Fedora 22 上使用 CPython 2.7.10 和 3.4.2 进行测试。
问题已在 IPython
github
https://github.com/ipython/ipython/issues/5828
它被认为是一个错误,但似乎没有很高的优先级。它只影响原始字符串。
我想不出它会困扰我的情况。我在哪里需要 'abc\\ndef'
?