[Python-Dev] PEP-498: Literal String Formatting

Chris Angelico rosuav at gmail.com
Mon Aug 17 16:58:21 CEST 2015


On Tue, Aug 18, 2015 at 12:50 AM, Barry Warsaw <barry at python.org> wrote:
> On Aug 17, 2015, at 11:02 AM, Paul Moore wrote:
>
>>    print(f"Iteration {n}: Took {end-start) seconds")
>
> This illustrates (more) problems I have with arbitrary expressions.
>
> First, you've actually made a typo there; it should be "{end-start}" -- notice
> the trailing curly brace.  Second, what if you typoed that as "{end_start}"?
> According to PEP 498 the original typo above should trigger a SyntaxError and
> the second a run-time error (NameError?).  But how will syntax highlighters
> and linters help you discover your bugs before you've even saved the file?
> Currently, a lot of these types of problems can be found much earlier on
> through the use of such linters.  Putting arbitrary expressions in strings
> will just hide them to these tools for the foreseeable future.  I have a hard
> time seeing how Emacs's syntax highlighting could cope with it for example.
>

The linters could tell you that you have no 'end' or 'start' just as
easily when it's in that form as when it's written out in full.
Certainly the mismatched brackets could easily be caught by any sort
of syntax highlighter. The rules for f-strings are much simpler than,
say, the PHP rules and the differences between ${...} and {$...},
which I've seen editors get wrong.

ChrisA


More information about the Python-Dev mailing list