New submission from Eric V. Smith <e...@trueblade.com>:

I originally propsed this here: 
https://mail.python.org/pipermail/python-ideas/2018-October/053956.html, and 
there has also been some discussion on discourse.

The proposal is to add a !d "type conversion" to f-strings, to make for simpler 
"print-based debugging". The idea is that !d would be like !r, etc., except 
that the resulting string would be:

- the text of the expression, followed by
- an equal sign, followed by
- the value of the expression

So this code:
s = 'A string'
result = f'{s!d}'

Would set result to 's="A string"'.

Note that the text of the expression is exactly what's between the
opening brace and the !. So:
result = f'{s !d}'

Would set result to 's ="A string"'. Note the space before the equal sign.

I can't decide if I'm going to allow a format specifier. If I do, it would 
apply to the entire resulting string. So:
result = f'{s!d:*^20}'

Would set result to '****s="A string"****'.

But I might reserve format specs for further use, perhaps to apply to the value 
of the expression instead of the resulting string.

Of course this is of most value for more complex expressions. This:
v = 3
result = f'{v*9+15!d}'

Would set result to 'v*9+15=42'.

I'd expect this mostly to be used with print() or logging, but it's a general 
f-string feature.

I have a patch almost ready.

----------
assignee: eric.smith
components: Interpreter Core
messages: 341261
nosy: eric.smith
priority: normal
severity: normal
stage: needs patch
status: open
title: f-strings: Add a !d conversion for ease of debugging
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36774>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to