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

Chris Angelico rosuav at gmail.com
Wed Oct 3 00:17:44 EDT 2018


On Wed, Oct 3, 2018 at 2:11 PM Anders Hovmöller <boxed at killingar.net> wrote:
>
>
> > This would be used in debugging print statements, that currently end up looking like:
> >
> > print(f'value={value!r}')
> >
> > and would now be:
> >
> > print(f'{value!d}')
>
> It seems to me that a short form for keyword arguments would improve this situation too. So instead of your suggestion one could do:
>
> print(dict(=value))
>
> And of course this feature wouldn’t be a minor feature on f-strings but a feature that is generally useful and composable so the above could be improved:
>
> def debug(**kwargs):
>     for k, v in kwargs.items():
>         print(f’{k}={v}’)
>
> debug(=value, =another)

What if it's not a simple name, though? The OP gave this (somewhat
simplistic, but indicative) example:

print(f'next: {value+1!d}')

AIUI, keyword arguments are all supposed to be legal names/atoms, so
you aren't supposed to do something like this:

debug(**{"value+1":value+1})

even though it does work in current versions of CPython. So even if
your "=value" syntax did permit it, I wouldn't want to guarantee that
in the language.

(Side point: Please watch your mailer. The debug() function above has
smart quotes in it, which means it can't be copied and pasted into the
interpreter. Not a big problem with trivially-simple functions, but if
it's something larger, it's annoying to have to track down
"SyntaxError: invalid character in identifier" to figure out why it's
not doing what you think it is.)

ChrisA


More information about the Python-ideas mailing list