f-string syntax deficiency?

Chris Angelico rosuav at gmail.com
Tue Jun 6 10:48:21 EDT 2023


On Wed, 7 Jun 2023 at 00:42, Roel Schroeven <roel at roelschroeven.net> wrote:
> (Recently there has been an effort to provide clearer and more useful
> error messages; this seems to be a case where there is still room for
> improvement: "SyntaxError: invalid syntax" doesn't immediately remind me
> of that fact that 'return' is a keyword and therefor can't be used as an
> attribute.)

That's true, but depending on exactly how you're seeing the error, it
might highlight the exact part that's a problem:

>>> opt.return
  File "<stdin>", line 1
    opt.return
        ^^^^^^
SyntaxError: invalid syntax

It's extremely hard to guess what the programmer might have intended
in these situations, as the error might not be the word "return" but
perhaps the punctuation (maybe that was supposed to be a semicolon, or
something). So Python does the best it can, and points out that the
"return" keyword in this context doesn't make syntactic sense.

ChrisA


More information about the Python-list mailing list