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

Eric V. Smith eric at trueblade.com
Tue May 7 21:23:35 EDT 2019


On 5/7/19 8:39 PM, Steven D'Aprano wrote:

Disclaimer: this topic seems to have been split over at least two issues on the bug tracker, a Python-Ideas thread from 2018, Discourse (I think...) and who knows what else. I haven't read it all, so excuse me if I'm raising something already discussed.

Yeah, it's all over the place. At this stage, I think discussing it here is best.

On Mon, May 06, 2019 at 08:39:41PM -0400, Eric V. Smith wrote:

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. You're going to hate me for bike-shedding, but I really don't like using = as a defacto unary postfix operator. To me, spam= is always going to look like it was meant to be spam== and the second half got accidentally deleted. I don't have a better suggestion, sorry.

I think = makes a great mnemonic for what's happening. I realize it looks a little funny at first, but just wait until you start confusing it with := (hah!). Everyone else I've discussed it with likes =.

In an earlier draft, back when this was spelled !d, you specifically talked about whitespace. Does this still apply?

Yes, it does.

spam = 42 f'{spam=}' # returns 'spam=42' f'{spam =}' # returns 'spam =42' f'{spam = }' # returns 'spam = 42' I guess?

All correct.

f'{spam+1=}' # returns 'spam+1=41' I guess?

I'm not proposing to redefine addition by one, so this is actually 'spam+1=43' :)

Eric



More information about the Python-Dev mailing list