[Python-ideas] Let’s make escaping in f-literals impossible
Chris Angelico
rosuav at gmail.com
Sun Aug 21 03:56:59 EDT 2016
On Sun, Aug 21, 2016 at 5:51 PM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> Speaking of which, how is this parsed?
> f"{'\n'}"
> If escape-handling is done first, the expression is a string literal holding
> an actual newline character (normally illegal), rather than an escape
> sequence which resolves to a newline character.
It's illegal.
> If that one somehow works, how about this?
> f"{r'\n'}"
Also illegal.
> I guess you'd have to write one of these:
> f"{'\\n'}"
> f"{'''\n''')"
> rf"{'\n'}"
Modulo the typo in the second one, these all result in the same code:
>>> dis.dis(lambda: f"{'\\n'}")
1 0 LOAD_CONST 1 ('\n')
2 FORMAT_VALUE 0
4 RETURN_VALUE
>>> f"{'\\n'}"
'\n'
ChrisA
More information about the Python-ideas
mailing list