[Python-ideas] f-string "debug" conversion
Eric V. Smith
eric at trueblade.com
Fri May 3 11:53:02 EDT 2019
> On Oct 4, 2018, at 6:24 PM, Tim Peters <tim.peters at gmail.com> wrote:
>
> [Tim]
>>
>> > Note that transforming
>> >
>> > {EXPR!d:FMT}
>> >
>> > into
>> >
>> > EXPR={repr(EXPR):FMT}
>> >
>> > is actually slightly more involved than transforming it into
>> >
>> > EXPR={EXPR:FMT}
>> >
>> > so I don't buy the argument that the original idea is simpler. More
>> > magical and less useful, yes ;-)
>
> [Eric V. Smith]
>> Actually, my proposal is to apply FMT to the entire result of
>> EXPR={repr(EXPR)}, not just the repr(EXPR) part. I'm not sure either is
>> particularly useful.
I think I’ve come around to Tim’s way of thinking on this. If the format spec is basically useless (which I think it would be if it applied to either the whole string or to the repr of the value), then what’s the point of allowing it? I also want the 90% case to be addressed by just plain repr.
I did consider (and implement) a version that prohibited format specs, but it seemed wrong to remove a feature that might have some clear use.
So, my compromise is:
- f"{expr!d}" expands to f"expr={repr(expr)}", but
- f"{expr!d:spec} expands to f"expr={format(expr, spec)}"
There’s some more discussion here:
https://bugs.python.org/issue36774#msg341306
As Paul Moore says on that issue, this is explained as: use repr, unless you specify a format spec, then use format.
> BTW, I checked, and I've never used !r, !s, or !a. So the idea that the format could apply to a string - when EXPR itself doesn't evaluate to a string - is simply foreign to me. I suppose it's natural to people who do use ![rsa] all the time - if such people exist ;-)
Especially when debugging and logging, I use !r all the time.
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190503/7ab7fc03/attachment.html>
More information about the Python-ideas
mailing list