[Python-ideas] keyword arguments everywhere (stdlib) - issue8706

Ethan Furman ethan at stoneleaf.us
Sun Mar 4 18:20:07 CET 2012


Guido van Rossum wrote:
> On Sat, Mar 3, 2012 at 7:46 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> The difficulty of implementing the Mapping ABC correctly in pure
>> Python is the poster child for why the lack of positional-only
>> argument syntax is a language wart - we define APIs (in C) that work
>> that way, which people then have to reconstruct manually in Python.
> 
> Nobody else seems to have seen the importance of solving *this*
> particular issue directly in the function signature -- but I
> personally support trying!
> 
>> My proposal is that we simply added a *third* alternative for "*args":
>> a full function parameter specification to be used to bind the
>> positional-only arguments.
>>
>> That is:
>>
>> 1. '*args' collects the additional positional arguments and places
>> them in a tuple
>> 2. '*' disallows any further positional arguments.
>> 3. '*(SPEC)' binds the additional positional arguments according to
>> the parameter specification.
>>
>> In all 3 cases, any subsequent parameter definitions are keyword only.
>>
>> The one restriction placed on the nested SPEC is that it would only
>> allow "*args" at the end. The keyword only argument and positional
>> only argument forms would not be allowed, since they would make no
>> sense (as all arguments to the inner parameter binding operation are
>> positional by design).

I don't understand -- example of what's allowed and not allowed?


> +1. This is certainly the most thorough solution for both problems at
> hand (simply requiring some parameters to be positional, and the
> specific issue when combining this with **kwds).

So a (more or less) complete rundown would look like this:

def foo(*(a, b)): ...  # all positional

def foo(*(a, b=1)): ... # all positional, b optional

def foo(*(a, b), c, d): ... # a, b positional; c, d required and keyword

def foo(*(a, b), c, d=1): ... # a, b positional; c required, d optional; 
c & d keyword

def foo(*(a, b=1), c=1, d=1): ...  # same, but b, c, d optional


If I understand correctly, there is no way to have positional-only, 
position-or-keyword, and keyword-only in the same signature?



More information about the Python-ideas mailing list