PEP 570: Demonstrate a corner case (#978)

The name of a positional-only parameter can occur in `**kwds`.
This commit is contained in:
Guido van Rossum 2019-04-06 14:43:10 -07:00 committed by GitHub
parent d3b0dfacba
commit f25f585af9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -514,6 +514,36 @@ and for ``varargslist`` would be::
| '**' vfpdef [','] | '**' vfpdef [',']
) )
--------------------
Semantic Corner Case
--------------------
The following is an interesting corollary of the specification.
Consider this function definition::
def foo(name, **kwds):
return 'name' in kwds
There is no possible call that will make it return ``True``.
For example::
>>> foo(1, **{'name': 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() got multiple values for argument 'name'
>>>
But using ``/`` we can support this::
def foo(name, /, **kwds):
return 'name' in kwds
Now the above call will return ``True``.
In other words, the names of positional-only parameters can be used in
``**kwds`` without ambiguity. (As another example, this benefits the
signatures of ``dict()`` and ``dict.update()``.)
---------------------------- ----------------------------
Origin of "/" as a Separator Origin of "/" as a Separator
---------------------------- ----------------------------