On Jul 14, 2019, at 00:33, Anders Hovmöller <[email protected]> wrote:
>
> This use case is nicely covered by short form for keyword arguments. So in
> this case instead of plot(x, z), we'd have plot(=x, =z) which would be
> transformed into plot(**{'x': x, 'z': z}) at compile time.
This does avoid the problem of needing new syntax, but it seems to have other
problems.
First, the function only gets the names, not live expressions that it can eval
late, or in a modified environment, etc., so it only handles this case, not
cases like df[year>1990].
Second, the names show up as keyword argument names, not as values. Besides
being a bit confusing to mix up names and values this way, it means everything
has to be defined as def plot(**kwargs) and document its actual arguments some
other way, and can’t annotate the parameter types, and if there are optional
like the ones pyplot takes for point formatting, scale, etc., there’s no
protection against them colliding with the names of the two fake-positional
args.
> This could work for any expression:
>
> foo(=lamda x: x*2) -> foo(**{'lamda x: x*2': lamda x: x*2})
Is this actually legal? The docs just say the contents of the ** mapping are
treated as additional keyword arguments. CPython happens to check that they are
strings but not check that those strings are valid keywords, but I don’t think
the language definition actually says this is the intended behavior, it’s just
an accident of the CPython implementation. So this might require at least
defining that implementation behavior as the only correct one, and changing the
docs to explain it.
Also, is this translation literally the way it’s implemented? Does that mean
bare keyword args can only appear in a call expression in the same place as **,
even though they don’t look similar?
> This feature is easy to implement and has broad applications.
How is this implemented? Doesn’t the compiler have the same problem generating
a string for the keyword out of an AST that the user code would have in the
OP’s proposal?
Also, what are the other applications besides the plot example and equivalent
things? I would guess the main uses are actually really simple cases. For
example, in a lot of short, simple functions, you often create variables named
x and y for the things you’re going to pass as the x and y arguments to plot,
and sometimes even xscale; similarly for the url argument to requests.get; and
so on. All of these get a few characters shorter to type and read (and maybe a
bit harder to accidentally screw up) if you can write “=x” instead of “x=x”.
But are there other benefits, for less trivial cases?
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/JLYJS2LKVVI4WUOQM2MNFUN7WPHLUDBG/
Code of Conduct: http://python.org/psf/codeofconduct/