[Python-ideas] Keyword only argument on function call

Jonathan Fine jfine2358 at gmail.com
Tue Sep 11 11:57:16 EDT 2018


Summary: locals() and suggestion __params__ are similar, and roughly
speaking each can be implemented from the other. Experts / pedants
would prefer not to use the name __params__ for this purpose.

Steve D'Aprano wrote:

> I imagine it would be fairly easy to fill in such a special __params__
> local variable when the function is called. The interpreter already has
> to process the positional and keyword arguments, it probably wouldn't be
> that hard to add one more implicitly declared local and fill it in:

[snip]

> Its also going to suffer from race conditions, unless someone much
> cleverer than me can think of a way to avoid them which doesn't slow
> down function calls even more.

As far as I know, locals() does not suffer from a race condition. But
it's not a local variable. Rather, it's a function that returns a
dict. Hence avoiding the race condition.

Python has some keyword identifiers. Here's one

    >>> __debug__ = 1
    SyntaxError: assignment to keyword


Notice that this is a SYNTAX error.  If __params__ were similarly a
keyword identifier, then it would avoid the race condition. It would
simply be a handle that allows, for example, key-value access to the
state of the frame on the execution stack. In other words, a
lower-level object from which locals() could be built.

By the way, according to

<quote>
https://www.quora.com/What-is-the-difference-between-parameters-and-arguments-in-Python

A parameter is a variable in a method definition. When a method is
called, the arguments are the data you pass into the method's
parameters.

Parameter is variable in the declaration of function. Argument is the
actual value of this variable that gets passed to function.
</quote>

In my opinion, the technically well-informed would prefer something
like __args__ or __locals__ instead of __params__, for the current
purpose.

Finally, __params__ would simply be the value of __locals__ before any
assignment has been done. Here's an example

    >>> def fn(a, b, c):
    ...    lcls = locals()
    ...    return lcls
    ...
    >>> fn(1, 2, 3)
   {'c': 3, 'b': 2, 'a': 1}

Note: Even though lcls is the identifier for a local variable, at the
time locals() is called the lcls identifier is unassigned, so not
picked up by locals().

So far as I can tell, __params__ and locals() can be implemented in
terms of each other. There could be practical performance benefits in
providing the lower-level command __params__ (but with the name
__locals__ or the like).

-- 
Jonathan


More information about the Python-ideas mailing list