[Python-Dev] PEP 498: Literal String Interpolation is ready for pronouncement

Nathaniel Smith njs at pobox.com
Sat Sep 5 19:28:27 CEST 2015


On Sep 5, 2015 9:20 AM, "Guido van Rossum" <guido at python.org> wrote:
>
> The processing of f-strings described by the PEP uses several phases:
>
> - find the end of the string (the final quote[s]) using the same
algorithm used for all string literals
> - expand \ escapes (e.g. \uXXXX)

It might be worth calling out explicitly in the text how this works for
what I suspect will be the most common case: embedded quoting embedded
quotes, like

    f"{ table[\"foo\"] }"

> - look for single {, then scan ahead to a matching } -- this skips
matching () [] {}

I assume this part of the algorithm uses the lexer and scans for matching
tokens rather than just scanning for characters? I tried looking at the PEP
to double check but couldn't find the relevant part. It's important for
things like

     f"{ '}' }"

> - look for optional !a,!r,!s and !<spec> inside each {...} pair
> - take what's left, enclose it in (), parse as expression

In fact maybe this whole list that Guido wrote should be copied into the
PEP; it's a good summary :-).

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150905/48c608ab/attachment.html>


More information about the Python-Dev mailing list