This PEP proposes a change to the way that function arguments areassigned to named parameter slots. In particular, it enables thedeclaration of “keyword-only” arguments: arguments that can onlybe supplied by keyword and which will never be automaticallyfilled in by a positional argument.
The current Python function-calling paradigm allows arguments tobe specified either by position or by keyword. An argument can befilled in either explicitly by name, or implicitly by position.
There are often cases where it is desirable for a function to takea variable number of arguments. The Python language supports thisusing the ‘varargs’ syntax (*name), which specifies that any‘left over’ arguments be passed into the varargs parameter as atuple.
One limitation on this is that currently, all of the regularargument slots must be filled before the vararg slot can be.
This is not always desirable. One can easily envision a functionwhich takes a variable number of arguments, but also takes oneor more ‘options’ in the form of keyword arguments. Currently,the only way to do this is to define both a varargs argument,and a ‘keywords’ argument (**kwargs), and then manually extractthe desired keywords from the dictionary.
Syntactically, the proposed changes are fairly simple. The firstchange is to allow regular arguments to appear after a varargsargument:
defsortwords(*wordlist,case_sensitive=False):...
This function accepts any number of positional arguments, and italso accepts a keyword option called ‘case_sensitive’. Thisoption will never be filled in by a positional argument, butmust be explicitly specified by name.
Keyword-only arguments are not required to have a default value.Since Python requires that all arguments be bound to a value,and since the only way to bind a value to a keyword-only argumentis via keyword, such arguments are therefore ‘required keyword’arguments. Such arguments must be supplied by the caller, andthey must be supplied via keyword.
The second syntactical change is to allow the argument name tobe omitted for a varargs argument. The meaning of this is toallow for keyword-only arguments for functions that would nototherwise take a varargs argument:
defcompare(a,b,*,key=None):...
The reasoning behind this change is as follows. Imagine for amoment a function which takes several positional arguments, aswell as a keyword argument:
defcompare(a,b,key=None):...
Now, suppose you wanted to have ‘key’ be a keyword-only argument.Under the above syntax, you could accomplish this by adding avarargs argument immediately before the keyword argument:
defcompare(a,b,*ignore,key=None):...
Unfortunately, the ‘ignore’ argument will also suck up anyerroneous positional arguments that may have been supplied by thecaller. Given that we’d prefer any unwanted arguments to raise anerror, we could do this:
defcompare(a,b,*ignore,key=None):ifignore:# If ignore is not emptyraiseTypeError
As a convenient shortcut, we can simply omit the ‘ignore’ name,meaning ‘don’t allow any positional arguments beyond this point’.
(Note: After much discussion of alternative syntax proposals, theBDFL has pronounced in favor of this ‘single star’ syntax forindicating the end of positional parameters.)
The previous section describes the difference between the oldbehavior and the new. However, it is also useful to have adescription of the new behavior that stands by itself, withoutreference to the previous model. So this next section willattempt to provide such a description.
When a function is called, the input arguments are assigned toformal parameters as follows:
In accordance with the current Python implementation, any errorsencountered will be signaled by raisingTypeError. (If you wantsomething different, that’s a subject for a different PEP.)
The function calling behavior specified in this PEP is a supersetof the existing behavior - that is, it is expected that anyexisting programs will continue to work.
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-3102.rst
Last modified:2025-02-01 08:59:27 GMT