[Python-ideas] Briefer string format

Stephen J. Turnbull stephen at xemacs.org
Tue Jul 21 07:16:24 CEST 2015

Eric V. Smith writes:

 > instead of trying to figure out that the numeric-looking '0' gets
 > converted to an integer, and the non-numeric-looking 'c' gets left as a
 > string. That logic already exists in str.format(), so let's just
 > leverage it from there.

Yes, please!  Guido's point that he wants no explicit use of locals(),
etc, in the implementation took me a bit of thought to understand, but
then I realized that it means a "macro" transformation with the
resulting expression evaluated in the same environment as an explicit
.format() would be.  And that indeed makes the whole thing as explicit
as invoking str.format would be.

I don't *really* care what transformations are used to get that
result, but DRYing this out and letting the __format__ method of the
indexed object figure out the meaning of the format string makes me
feel better about my ability to *think* about the meaning of an f"..." 

In particular, isn't it possible that a user class's __format__ might
decide that *all* keys are strings?  I don't see how the
transformation Steve Dower proposed can possibly deal with that

Another conundrum is that it's not obvious whether f"{a[01]}" is a
SyntaxError (as it is with str.format) or equivalent to
"{}".format(a['01']) (as my hypothetical user's class would expect).

More information about the Python-ideas mailing list