[Python-ideas] Make non-meaningful backslashes illegal in string literals

random832 at fastmail.us random832 at fastmail.us
Sat Aug 8 07:52:53 CEST 2015


On Sat, Aug 8, 2015, at 00:56, Andrew Barnert via Python-ideas wrote:
> Which most languages? In C, sh, perl, and most of their respective
> descendants, it means x.

In C it is undefined behavior. Many compilers will provide a warning,
even for extensions they do define such as \e.

C incidentally provides \u at a lower level than string literals (they
can appear anywhere in source code), and it may not specify most ASCII
characters, even in string literals.

In POSIX sh, there is no support for any special backslash escape.
Backslash before _any_ character outside of quotes makes that character
literal - That is, \n is n, not newline. I wouldn't really regard this
as the same kind of context. For completeness, I will note that inside
double quotes, backslash before any character it is not required to
escape (such as ` " or $) incudes the backslash in the result. Inside
single quotes, backslash has no special meaning at all.

In POSIX echo, the behavior is implementation-defined. Some existing
implementations include the backslash like python.

In POSIX printf, the behavior is unspecified. Some existing
implementations include the backslash.

In ksh $'strings', it means the literal character, no backslash.

In bash $'strings', it includes the backslash.


More information about the Python-ideas mailing list