[Python-Dev] PEP 498 f-string: is it a preprocessor?

Victor Stinner victor.stinner at gmail.com
Mon Aug 10 16:18:36 CEST 2015


I read the PEP but I don't understand how it is implemented. For me, it
should be a simple preprocessor:

- f'x={x}' is replaced with 'x={0}'.format(x) by the compiler
- f'x={1+1}' is replaced with 'x={0}'.format(1+1)
- f'x={foo()!r}' is replaced with 'x={0!r}'.format(foo())
- ...

That's all. No new language, no new function or method.

It's unclear to me if arbitrary expressions should be allowed or not. If
not, we may pass parameters by keywords instead:

f'x={x}' is replaced with 'x={x}'.format(x=x) by the compiler

'...'.format(...) is highly optimized. In the current PEP, I see that each
parameter is rendered in its own independent buffer (.__format__() method
called multiple times) and then concateneted by ''.join(...). It's less
efficient that using a single call to .format().


PS: it looks like the gmail application changed the font size in the middle
of my email. I don't know how to use plain text, sorry about that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150810/a6230dac/attachment.html>

More information about the Python-Dev mailing list