[Python-ideas] f-string "debug" conversion

Eric V. Smith eric at trueblade.com
Thu May 2 08:24:11 EDT 2019

See https://bugs.python.org/issue36774.

On 10/2/18 8:27 PM, Eric V. Smith wrote:
> This idea was proposed to me at the core sprints last month by Larry
> Hastings. I've discussed it with a few people, who seem generally
> positive about it, and we've tweaked it a little bit. I've spent some
> time implementing it, and I think it's doable. I thought I'd post it
> here for any additional feedback.
> Here’s the idea: for f-strings, we add a !d conversion operator, which
> is superficially similar to !s, !r, and !a. The meaning of !d is:
> produce the text of the expression (not its value!), followed by an
> equal sign, followed by the repr of the value of the expression. So:
> value = 10
> s = 'a string!'
> print(f'{value!d}')
> print(f'next: {value+1!d}')
> print(f'{s!d}')
> produces:
> value=10
> next: value+1=11
> s='a string!'
> I’m not proposing this for str.format(). It would only really make sense
> for named arguments, and I don’t think
> print('{value!d}'.format(value=value) is much of a win.
> The result is a string, so if you really wanted to, you could use a
> string formatting spec. So:
> print(f'*{value!d:^20}*'
> would produce:
> *      value=10      *
> Although I don’t think that would be very useful in general.
> The mnemonic is !d for “debugging”. I’d wanted to use !=, because
> there’s an equal sign involved in the result, but = is the one character
> that can’t be used after ! (it’s “not equal” in expressions, and
> f-strings look specifically for that case). I also mentioned !!, but I
> think I prefer !d as being less confusing.
> This would be used in debugging print statements, that currently end up
> looking like:
> print(f'value={value!r}')
> and would now be:
> print(f'{value!d}')
> There have been discussions about ways to specify str() vs. repr(),
> using characters other than '=', adding spaces, etc. But they all end up
> over-complicating what should be a simple tool, not a Swiss Army knife.
> Thoughts?
> Eric
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list