From f25f585af92a05f975db0c10e740a5fa783d7aaf Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 6 Apr 2019 14:43:10 -0700 Subject: [PATCH] PEP 570: Demonstrate a corner case (#978) The name of a positional-only parameter can occur in `**kwds`. --- pep-0570.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pep-0570.rst b/pep-0570.rst index d307c48e3..4f94ed7ff 100644 --- a/pep-0570.rst +++ b/pep-0570.rst @@ -514,6 +514,36 @@ and for ``varargslist`` would be:: | '**' 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 "", line 1, in + 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 ----------------------------