[Python-ideas] f-string "debug" conversion
Eric V. Smith
eric at trueblade.com
Wed Oct 3 06:44:37 EDT 2018
On 10/3/2018 3:54 AM, Steven D'Aprano wrote:
> On Tue, Oct 02, 2018 at 08:27:03PM -0400, Eric V. Smith wrote:
>
>> 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!),
>
> I SO WANT THIS AS A GENERAL FEATURE, not just for f-strings, it hurts.
>
> Actually what I want is an executable object (a function?) which has the
> AST and text of the expression attached. If putting this into f-strings
> is a first step towards getting this thunk-like thing, then I don't
> need to read any further, I'm +10000 :-)
I feel your pain, but we're a long way from that.
>> produces:
>>
>> value=10
>> next: value+1=11
>> s='a string!'
>
> I can see lots of arguments about whether the equals sign should have
> spaces around it.
>
> Maybe !d for no spaces and !D for spaces?
>
> print(f'next: {value+1!d}')
> print(f'next: {value+1!D}')
>
> would print
>
> next: value+1=11
> next: value+1 = 11
I'm not sure it's worth the hassle. I considered a whole format spec
language for this, but it sort of got out of hand.
>> 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.
>
> I'm not so sure that it only makes sense for named arguments. I think it
> works for arbitrary expressions too:
>
> f'{len("NOBODY expects the Spanish Inquisition!")!d}'
>
> ought to return
>
> 'len("NOBODY expects the Spanish Inquisition!")=39'
>
> which I can see being very useful in debugging expressions.
Yes, that's part of the proposal, but I wasn't very clear. That's what
the "value+1" example was supposed to convey. My comment about named
parameters applied only to str.format(), and why it won't be supported
there.
Eric
More information about the Python-ideas
mailing list