FAQ

On 9/5/2015 12:18 PM, Guido van Rossum wrote:
On Sat, Sep 5, 2015 at 2:10 AM, haypo s <victor.stinner at gmail.com
wrote:
We already went over this. You might as well argue that __import__ or
lambda should not be used as arguments to print(). It's an expression,
and it must allow exactly everything that is allowed in other places
where expressions are allowed.
(is it possible to indent and comment code inside a f-string?)

I cannot think of any expression where indents on continuation lines
have any significance other than being extra whitespace. Comments are
allowed, but cannot cause line continuation by ending with \.

(1 + #\
... 2)
3
1 + #\
    File "<stdin>", line 1
      1 + #\
           ^
SyntaxError: invalid syntax

Now that's an interesting question. I think the answer must be No,

This is a restriction on 'exactly everything'. The doc should then say
'any expression without embedded comments' (plus the usual restriction
on not having embedded strings using the same quotes as the string itself).

because we don't want to deal with ambiguities like whether a closing
curly bracket or string quote should be ignored inside such comments.
The processing of f-strings described by the PEP uses several phases:

- find the end of the string (the final quote[s]) using the same
algorithm used for all string literals
- expand \ escapes (e.g. \uXXXX)
- look for single {, then scan ahead to a matching } -- this skips
matching () [] {}
- look for optional !a,!r,!s and !<spec> inside each {...} pair
- take what's left, enclose it in (), parse as expression

The presence of comments would impede several of these stages.

--
Terry Jan Reedy

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2017 Grokbase