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

Chris Angelico rosuav at gmail.com
Sat Sep 5 12:57:25 CEST 2015


On Sat, Sep 5, 2015 at 7:10 PM, haypo s <victor.stinner at gmail.com> wrote:
> 2015-09-05 5:01 GMT+02:00 Guido van Rossum <guido at python.org>:
>> And I'm ready to accept it. I'll wait until Tuesday night (Monday's a
>> holiday in the US) in case anything unforeseen comes up, but this is really
>> the Last Call for this PEP.
>
> Would it be possible to specify a subset of the Python language
> allowed in f-string? For example, __import__ or lambda should not be
> used in a f-string. I'm not convinced that a loop or
> list/dict/set-comprehension is a good idea neither.

PEP 8 and other style guides will quite possibly pick up some sort of
guidelines on the subject. The way I see it, though, it's like the
difference between lambda and def - there's a technical difference
(lambda and f-strings are restricted to expressions, so if you need
statements, pull it out of line), but for the rest, it's a judgement
call  as to how big your expression should be. The interpreter doesn't
need to be involved in that decision.

> I would prefer to keep as much code as possible *outside* f-string because:
> - text editor knows well how to color it
> - static analyzers know how to analyze it
> - it encourage developers to indent and comment their code correctly,
> whereas f-string has more restrictons on indentation (is it possible
> to indent and comment code inside a f-string?)

If it can be done inside parentheses, it can be done inside an
f-string. Though I suspect multi-line expressions will generally be a
bad idea - it'll be harder to see how much text is literal and how
much is the expression. But again, question for the style guides not
the interpreter.

Text editors and static analyzers will simply need to be taught that
f-strings are special forms of expression.

> Well, I'm not convinced that we need a larger subset than what is
> allowed currently in str.format(), simple expressions like: obj.attr,
> obj[index], etc.
> ...
> IMHO the main advantage of allowing expressions inside f-string is
> that it adds something really new compared to the current str.format()
> method. Without it, f-string are less interesting.

Putting these two paragraphs together: you're not convinced we need
f-strings, is that correct? I agree with your conclusion paragraph,
and feel that they *are* interesting and useful in their
currently-described state. Having full expression syntax available is
definitely worth while; it's exactly the same benefit as being able to
use lambda inside a function call, or anything else that we can do
inline.

ChrisA


More information about the Python-Dev mailing list