[Python-ideas] f-string "debug" conversion
Steven D'Aprano
steve at pearwood.info
Wed Oct 3 04:05:39 EDT 2018
On Wed, Oct 03, 2018 at 06:57:19AM +0200, Anders Hovmöller wrote:
> debug(next=value+1)
>
> Still shorter than the proposed syntax
Are we trying to emulate Perl now? *wink*
> and much more readable.
So you say.
To me that looks like a regular function call, which calls an ordinary
function "debug" and takes a simple keyword argument next with value
"value+1".
Things which contain compiler magic should look special, not like
ordinary function calls.
> > 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})
>
> Really? That seems pretty weird to me. I’ve used that type of thing in
> production code from time to time.
The fact that this works is, I think, an accident of implementation:
py> def spam(**kw):
... print(kw)
...
py> spam(**{"value+1": 42})
{'value+1': 42}
rather than a guaranteed language feature. I can't find any relevent
documentation on it, but I'd be very wary about relying on it.
(To be honest, I expected it to fail until I tried it.)
You certainly can't do this:
py> spam(value+1=42)
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
--
Steve
More information about the Python-ideas
mailing list