Message341261
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. |
|
Date |
User |
Action |
Args |
2019-05-02 11:40:45 | eric.smith | set | recipients:
+ eric.smith |
2019-05-02 11:40:45 | eric.smith | set | messageid: <1556797245.95.0.619255948077.issue36774@roundup.psfhosted.org> |
2019-05-02 11:40:45 | eric.smith | link | issue36774 messages |
2019-05-02 11:40:45 | eric.smith | create | |
|