[Python-Dev] Easier debugging with f-strings

Eric V. Smith eric at trueblade.com
Mon May 6 20:39:41 EDT 2019


Last fall Larry Hastings made a suggestion for adding a way to make 
so-called "print-based debugging" easier with f-strings. Basically the 
approach is that f-strings would be able to produce the text of the 
expression and the value of that expression, without repeating the 
expression in the f-sting. No more writing f'foo={foo}, bar={bar}'. foo 
and bar should each only be in there once each!

At PyCon US 2019 I did a lightning talk about this, suggesting the 
syntax of !d, so that if foo="Hello", then f"{foo!d}" would produce 
"foo='Hello'". That is, it's the text of the expression, followed by an 
equal sign, followed by the repr of the expression. I have implemented 
this and a PR exists. Arbitrary expressions are allowed. I heard from 
core devs and end users after this talk, and all were positive.

After that lightning talk, Larry and I talked about it some more, and 
for a number of reasons decided that it would make more sense if the 
syntax used an = sign. So we came up with f"{foo=}", which would also 
produce "foo='Hello'".

The reasons for the change are:
- Having '=' in the expression is a better mnemonic than !d.
- By not using a conversion starting with !, we can compose = with the 
existing ! conversions, !r, !s, and the rarely used !a.
- We can let the user have a little more control of the resulting string.

Another thing I like about this approach over !d is that the patch is 
simpler, because there are fewer special cases. And because there are 
fewer special cases in the code, I think the proposal is easier to 
explain than !d (in particular how it interacts (or doesn't!) with 
format specifiers).

There's a good rationale here, along with a PR: 
https://bugs.python.org/issue36817.

My plan is to commit this change before 3.8b1. If anyone would like to 
discuss it at PyCon, I'll be around until about 10:30 am on Tuesday. 
I'll be in the CPython sprint room, and I'll be watching bpo, too.

Eric


More information about the Python-Dev mailing list