[Python-ideas] String interpolation for all literal strings

MRAB python at mrabarnett.plus.com
Thu Aug 6 02:28:16 CEST 2015


On 2015-08-05 21:03, Guido van Rossum wrote:
> On Wed, Aug 5, 2015 at 9:34 PM, Yury Selivanov <yselivanov.ml at gmail.com
> <mailto:yselivanov.ml at gmail.com>> wrote:
>
>     On 2015-08-05 2:56 PM, Eric V. Smith wrote:
>
>         In the "Briefer string format" thread, Guido suggested [1] in
>         passing
>         that it would have been nice if all literal strings had always
>         supported
>         string interpolation.
>
>         I've come around to this idea as well, and I'm going to propose
>         it for
>         inclusion in 3.6. Once I'm done with my f-string PEP, I'll consider
>         either modifying it or creating a new (and very similar) PEP.
>
>         The concept would be that all strings are scanned for \{ and }
>         pairs. If
>         any are found, then they'd be interpreted in the same was as the
>         other
>         discussion on "f-strings". That is, the expression between the
>         \{ and }
>         would be extracted and searched for conversion characters and format
>         specifiers. The expression would be evaluated, converted if
>         needed, have
>         its __format__ method called, and the resulting string inserted
>         back in
>         to the original string.
>
>         Because strings containing \{ are currently valid, we'd have to
>         introduce this feature with a __future__ import statement. How we
>         transition to having this be the default interpretation of
>         strings is up
>         in the air.
>
>
>     Have you considered using '#{..}' syntax (used by Ruby and
>     CoffeeScript)?
>
>
> Well, I feel bound by *some* backward compatibility... Python string
> literals don't treat anything special except \ followed by certain
> characters. It feels better to add to the set of "certain characters"
> (which we've done before) than to add a completely new escape sequence.
>
>     '\{..}' feels unbalanced and weird.
>
>
> Not more or less than '#{..}'. I looked through
> https://en.wikipedia.org/wiki/String_interpolation for what other
> languages do, and it reminded me that Swift uses '\(..)' -- that would
> also be a possibility, but '\{..}' feels closer to the existing PEP 3101
> '{..}.format(..) syntax.
>
What that page shows me is how common it is to use $ for interpolation;
it's even used in Python's own string.Template!

> And I did indeed mean for r-strings not to be interpolated (since they
> are exempt from \ interpretation).
>
> We should look a bit more into how this proposal interacts with regular
> expressions (where \{ can be used to avoid the special meaning of {..}).
> I think \(..) would be more cumbersome than \{..}, since () is more
> common in regular expressions than {}.
>
> BTW an idea on the transition: with a __future__ import \{..} is enabled
> in all non-raw strings, without a __future__ import you can still use
> \{..} inside f-literals. (Because having to add a __future__ import
> interrupts one's train of thought.)
>
I'd prefer interpolated string literals to be marked, leaving unmarked
literals as they are (except for rejecting unknown escapes!).


More information about the Python-ideas mailing list