[Python-Dev] Easier debugging with f-strings (original) (raw)

Tim Peters tim.peters at gmail.com
Tue May 7 12:50:54 EDT 2019


[Larry Hastings <larry at hastings.org>]

Guido just stopped by--we're all at the PyCon 2019 dev sprints--and we had a chat about it. Guido likes it but wanted us to restore a little of the magical behavior we had in "!d": now, = in f-strings will default to repr (!r), unless you specify a format spec. If you specify a format spec it will always default to format. And naturally if you specify an explicit conversion function (!r !s !a) it will use that.

This makes !f irrelevant, so we're removing it. Here's the thinking: 99% of the time the user will just use {foo=}, and for that you want repr. After that, 0.99% of the time the user will want a format spec that applies directly the value. It's exceedingly unlikely that someone will want a format spec, but want it to apply to repr(value) and not value itself. So that's possible (f'{foo=!r:20}'). But the default behavior is the most common case at every step.

+1. Perfect! I'm one of the 0.99% who will frequently use this to display floats, and really wants them to show as "0.99%" rather than "0.9913499340289%".

BTW, that Guido person has made enough decent contributions by now that I think he should be asked whether he wants to become a core dev!



More information about the Python-Dev mailing list