[Python-Dev] PEP 3102: Keyword-only arguments

Talin talin at acm.org
Sun Apr 30 22:14:42 CEST 2006


Zachary Pincus <zpincus <at> stanford.edu> writes:

> That seems a bit odd, as my natural expectation wouldn't be to see  
> kw1 ands kw2 as required, no-default keyword args, but as misplaced  
> positional args.
> 
> Perhaps this might be a little better?
>    def foo(*args, kw1=, kw2=):
> I'm rather not sure. At least it makes it clear that kw1 and kw2 are  
> keyword arguments, and that they have no default values.
> 
> Though, I'm kind of neutral on the whole bit -- in my mind "keyword  
> args" and "default-value args" are pretty conflated and I can't think  
> of any compelling reasons why they shouldn't be. It's visually handy  
> when looking at some code to see keywords and be able to say "ok,  
> those are the optional args for changing the handling of the main  
> args". I'm not sure where the big win with required keyword args is.

I think a lot of people conflate the two, because of the similarity in
syntax, however they aren't really the same thing. In a function
definition, any argument can be a keyword argument, and the
presence of '=' means 'default value', not 'keyword'.

I have to admit that the primary reason for including required
keyword arguments in the PEP is because they fall out naturally
from the implementation. If we remove this from the PEP, then
the implementation becomes more complicated - because now you
really are assigning a meaning to '=' other than 'default value'.

>From a user standpoint, I admit the benefits are small, but I don't
see that it hurts either. The only use case that I can think of is
when you have a function that has some required and some optional
keyword arguments, and there is some confusion about the ordering
of such arguments. By disallowing the arguments to be positional,
you eliminate the possibility of having an argument be assigned
to the incorrect parameter slot.

> For that matter, why not "default positional args"? I think everyone  
> will agree that seems a bit odd, but it isn't too much odder than  
> "required keyword args". (Not that I'm for the former! I'm just  
> pointing out that if the latter is OK, there's no huge reason why the  
> former wouldn't be, and that is in my mind a flaw.)
> 
> Second:
> >   def compare(a, b, *, key=None):
> This syntax seems a bit odd to me, as well. I always "understood" the  
> *args syntax by analogy to globbing -- the asterisk means to "take  
> all the rest", in some sense in both a shell glob and *args.
> 
> In this syntax, the asterisk, though given a position in the comma- 
> separated list, doesn't mean "take the rest and put it in this  
> position." It means "stop taking things before this position", which  
> is a bit odd, in terms of items *in* an argument list.
> 
> I grant that it makes sense as a derivation from "*ignore"-type  
> solutions, but as a standalone syntax it feels off. How about  
> something like:
>    def compare(a, b; key=None):

I wanted the semicolon as well, but was overruled. The current
proposal is merely a summary of the output of the discussions
so far.

-- Talin



More information about the Python-Dev mailing list