[Python-ideas] f-string "debug" conversion

Tim Peters tim.peters at gmail.com
Wed Oct 3 20:01:39 EDT 2018


[Tim]

> >> ....  But what if

>>
> >>      {EXPR!d:FMT}
> >>
> >> acted like the current
> >>
> >>      EXPR={EXPR:FMT}
> >>
> >> ?  I'd find _that_ useful often.  For example, when displaying floats,
> >> where the repr is almost never what I want to see.
> >>  ...


[Eric V. Smith <eric at trueblade.com>]

> After giving this some more thought, the problem with this approach is
> that there's no way to get the repr of the object, which for debugging
> can be pretty useful (think strings).


Sure there is:

    f"{repr(var)!d"}

would expand to, as a matter of course (nothing special about it):

    f"{repr(var)={repr(var)}"

which would yield, e.g.,

    repr(var)=`a\n'

Special shortcuts for calling `repr()` went out of style when Guido got rid
of that meaning for the backtick symbol ;-)

Remember, by default
> object.__format__ calls object.__str__.
>

Which - since there's already a related default - makes it a Dubious Idea
to make some other spelling use a different default.



> I guess we could get around this by combining !d and !r and assigning
> some meaning to that, which I'd rather not do.
>
> Or, this occurred to me right before hitting "send": if there's no
> format spec, use repr(expr). If there is one (even if it's zero-length),
> use format(expr, spec). I'll have to think on that for a while. Maybe
> there's too much voodoo going on there.
>

 The alternative:  if I want repr,(which I usually don't), make me call
"repr()" (which I don't mind at all).

If there must be a shortcut, "!dr" or "!rd" are at least cryptically
explicit, and

    {EXPR!dr}

would expand to

    EXPR={repr(EXPR)}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181003/a372a693/attachment.html>


More information about the Python-ideas mailing list