print(f'{value!d}') is a lot of symbols and boilerplate to type out just
for a debugging statement that will be deleted later. Especially now that
breakpoint() exists, I can't really see myself using this.
I also don't see the use case of it being within an f-string, because I've
never had to interpolate a debug string within some other string or format
it in a fancy way. You said it yourself, taking advantage of other f-string
features isn't very useful in this case.
If other people can find a use for it, I'd suggest making it ita own
function -- debug(value) or something similar.
David
On Tue, Oct 2, 2018, 8:27 PM Eric V. Smith
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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/