On 21.04.2020 04:25, Andrew Barnert via Python-ideas wrote:
On Apr 20, 2020, at 16:24, M.-A. Lemburg firstname.lastname@example.org wrote:
On 20.04.2020 19:43, Andrew Barnert via Python-ideas wrote:
On Apr 20, 2020, at 01:06, M.-A. Lemburg email@example.com wrote:
The current version already strikes me as way too complex. It's by far the most complex piece of grammar we have in Python:
funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite
But nobody’s proposing changing the function definition syntax here, only the function call syntax. Which is a lot less hairy. It is still somewhat hairy, but nowhere near as bad, so this argument doesn’t really apply.
True, I quoted the wrong part of the grammar for the argument, sorry. I meant this part:
which is simpler, but not really much, since the devil is in the details.
Let’s just take one of the variant proposals under discussion here, adding ::identifier to dict displays. This makes no change to the call grammar, or to any of the call-related bits, or any other horribly complicated piece of grammar. It just changes key_datum (a nonterminal referenced only in dict_display) from this:
expression ":" expression | “**” or_expr
… to this:
expression ":" expression | “::” identifier | “**” or_expr
That’s about as simple as any syntax change ever gets.
Which is still not nothing. But you’re absolutely right that a big and messy change to function definition grammar would have a higher bar to clear than most syntax proposals—and for the exact same reason, a small and local change to dict display datum grammar has a lower bar than most syntax proposals.
I think the real issue you would like to resolve is how to get at the variable names used for calling a function, essentially pass-by-reference (in the Python sense, where variable names are references to objects, not pointers as in C) rather than pass-by-value, as is the default for Python functions.
The f-string logic addresses a similar need.
With a way to get at the variable names used for calling a function from inside a function, you could then write a dict constructor which gives you the subset of vars() you are looking for.
If you know that the variable names used in the function are the same as in the calling context, you can already do this using introspection.
For the general purpose case, I don't think this is possible without the help of the compiler, since the pass-by-value is implemented using the bytecodes and the stack, AFAIR.
Rather than using new syntax, a helper in the compiler would do the trick, though, similar to what e.g. @classmethod does.