[Python-Dev] PEP 498 f-string: is it a preprocessor?
Eric V. Smith
eric at trueblade.com
Tue Aug 11 00:59:29 CEST 2015
On 8/10/2015 6:22 PM, Victor Stinner wrote:
> Le lundi 10 août 2015, Eric V. Smith <eric at trueblade.com
> <mailto:eric at trueblade.com>> a écrit :
>
> On 08/10/2015 10:18 AM, Victor Stinner wrote:
> > Hi,
> >
> > 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.
>
> There is no new function or method being proposed. The "pre-processor"
> is being implemented as the ast is being built. As the PEP says, though,
> the expressions supported aren't exactly the same, so a simple
> conversion to str.format syntax isn't possible.
>
>
> Can you please provide example(s) of f-string(s) which cannot be
> replaced by a call to .format() like I did?
Oops, I was thinking of going the other way (str.format -> f''). Yes, I
think you're correct.
But in any event, I don't see the distinction between calling
str.format(), and calling each object's __format__ method. Both are
compliant with the PEP, which doesn't specify exactly how the
transformation is done.
Eric.
More information about the Python-Dev
mailing list