PEP 570: Demonstrate a corner case (#978)
The name of a positional-only parameter can occur in `**kwds`.
This commit is contained in:
parent
d3b0dfacba
commit
f25f585af9
30
pep-0570.rst
30
pep-0570.rst
|
@ -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
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
Loading…
Reference in New Issue