[Python-ideas] Dart like multi line strings identation

Chris Angelico rosuav at gmail.com
Sun Apr 1 05:24:29 EDT 2018


On Sun, Apr 1, 2018 at 7:11 PM, Eric V. Smith <eric at trueblade.com> wrote:
> Interestingly, in 2.7 'ur' is a valid prefix, but not in 3.6. I don't recall
> if that was deliberate or not. And 'ru' isn't valid in either version.

I believe it was. The 'ur' string literal in Py2 was a bizarre hybrid
of raw-but-allowing-Unicode-escapes, which makes no sense in the Py3
world.

$ python3
Python 3.8.0a0 (heads/literal_eval-exception:ddcb2eb331, Feb 21 2018, 04:32:23)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(u"\\ \u005c \\")
\ \ \
>>> print(r"\\ \u005c \\")
\\ \u005c \\

$ python2
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print(u"\\ \u005c \\")
\ \ \
>>> print(r"\\ \u005c \\")
\\ \u005c \\
>>> print(ur"\\ \u005c \\")
\\ \ \\

In Py3, a normal Unicode literal (with or without the 'u' prefix,
which has no meaning) will interpret "\u005c" as a backslash. A raw
literal will treat it as a backslash followed by five other
characters. In Py2, the same semantics hold for normal Unicode
literals, and for bytes literals, the "\u" escape code has no meaning
(and is parsed as "\\u"). But in a raw Unicode literal, the
backslashes are treated literally... unless they're starting a "\u"
sequence, in which case they're parsed.

So if you use a raw string literal in Python 3 to store a Windows path
name, you're fine as long as it doesn't end with a backslash (a wart
in the design that probably can't be done any other way). But in Py2,
a raw Unicode literal will occasionally misbehave in the *exact*
*same* *way* as a non-raw string literal will - complete with it being
data-dependent.

Since the entire point of the Py3 u"..." prefix is compatibility with
Py2, the semantics have to be retained. There's no point supporting
ur"..." in Py3 if it's not going to produce the same result as in Py2.

ChrisA


More information about the Python-ideas mailing list