[Python-Dev] Positional-only parameters in Python
Larry Hastings
larry at hastings.org
Thu Jan 18 04:26:44 EST 2018
On 01/17/2018 08:29 AM, Ethan Furman wrote:
> On 01/17/2018 08:14 AM, Serhiy Storchaka wrote:
>> 17.01.18 16:34, Victor Stinner пише:
>>> In Februrary 2017, I proposed on python-ideas to change the Python
>>> syntax to allow to declare positional-only parameters in Python:
>>>
>>> https://mail.python.org/pipermail/python-ideas/2017-February/044879.html
>>>
>>> https://mail.python.org/pipermail/python-ideas/2017-March/044956.html
>>
>> The main problem -- designing a syntax that does not look ugly.
>
> The syntax question is already solved:
>
> def some_func(a, b, /, this, that, *, the_other):
> # some stuff
>
> Everything before the slash is positional-only, between the slash and
> star is positional-or-keyword, and after the star is keyword-only.
> This is what is in our generated help(), and there is a nice symmetry
> between '/' and '*' being opposites, and positional/keyword being
> opposites. And slash is certainly no uglier than star. ;)
To clarify: this is the syntax used by "Argument Clinic", both as its
input language, and as part of its output, exposed via the
__text_signature__ attribute on builtins.
Why did Argument Clinic choose that syntax? It was suggested by one
Guido van Rossum in March 2012:
https://mail.python.org/pipermail/python-ideas/2012-March/014364.html
https://mail.python.org/pipermail/python-ideas/2012-March/014378.html
https://mail.python.org/pipermail/python-ideas/2012-March/014417.html
I'm not wading into the debate over what syntax Python should use if it
adds positional-only parameters, except to say that I think "/" is
reasonable. If Python winds up using a different syntax, I'd look into
modifying Argument Clinic so that it accepts both this hypothetical new
syntax and the existing syntax using "/".
Would we be adding yet a third argument-parsing function,
PyArg_ParseTupleAndKeywordsWithPositionalOnly()? I would actually
propose a different approach: modify Argument Clinic so it generates
custom argument-parsing code for each function, adding a new call type
(which I propose calling "METH_RAW" or "METH_STACK") where the stack is
passed in directly. I spent some time on this in the past, though I got
distracted and now haven't touched it in years.
Cheers,
//arry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180118/c0818acf/attachment.html>
More information about the Python-Dev
mailing list