[issue36774] f-strings: Add a !d conversion for ease of debugging

Paul Moore report at bugs.python.org
Thu May 2 15:41:42 EDT 2019


Paul Moore <p.f.moore at gmail.com> added the comment:

> So the question is: how do you get repr by default, but allow the format spec?
>
> The only thing I've come up with is:
> - f"{expr!d}" expands to f"expr={repr(expr)}", but
> - f"{expr!d:spec} expands to f"expr={format(expr, spec)}"
>
> I think this is the most useful version. But is it too complex to explain?

Agreed, this is the most useful version. Not only do I not think it's
too complicated to explain, I actually think it's the obvious
behaviour, and what people would expect even without an explanation.

If asked, I'd explain it as:

    f"{expr!d:spec}" expands to "expr=<the value of expr, formatted
using spec>". If ":spec" is omitted, repr() is used.

That seems simple enough to me - the key is that we're just saying "if
:spec is omitted, we use repr".

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36774>
_______________________________________


More information about the Python-bugs-list mailing list