[Python-3000] PEP 3102

Arnaud Delobelle arnodel at googlemail.com
Sun Feb 17 18:45:48 CET 2008



On Feb 14, 11:13 pm, "Guido van Rossum" <gu... at python.org> wrote:
> On Thu, Feb 14, 2008 at 2:48 PM, Raymond Hettinger <pyt... at rcn.com> wrote:
> > I've been exercising the new keyword-only arguments syntax.  It is absolutely wonderful.  I'm amazed at how many long standing problems it solves elegantly.
>
> Agreed. Now can you come up with a syntax for positional-only
> arguments? So far everybody has failed at that, and there are some use
> cases where it's useful too.

The idea in PEP 3102 is that *args gobbles up all remaining positional
arguments, hence what comes after can only be interpreted as
keyword-only arguments. This is extended to allowing * on its own.

e.g. f(*, z, t=2)
     => z, t are keyword-only

Following the same train of thought, **kwargs gobbles up all remaining
keyword arguments, hence what comes after can only interpreted as
positional-only arguments.  Then one could extend it to allowing ** on
its own.

e.g. f(**, x, y=1)
     => x, y are positional-only

Now, how do keyword-only arguments fit in?  Say x, y=1 are
positional-only and z, t=2 are keyword-only.  One could have:

f(**, x, y=1, *, z, t=2)

The general cases could look like

f(x, y=1, *args, z, t=2, **kwargs):
     => x, y are hybrid
     => args: all remaining positional arguments
     => z, t are keyword-only
     => kwargs: all remaining keyword arguments

f(**kwargs, x, y=1, *args, z, t=2):
     => x, y are positional-only
     => args: all remaining positional arguments
     => z, t are keyword-only
     => kwargs: all remaining keyword arguments

'args' and 'kwargs' are optional in both cases. If 'kwargs' is present
in the second version, it is a bit strange as it usually only appears
at the end.  Alternatively, only '**' could be allowed at the start
and if needed '**kwargs' would come at the end:

f(**, x, y=1, *args, z, t=2, **kwargs)

Note that this syntax doesn't allow for positional-only and hybrid
arguments to coexist.  But this is nice IMHO, as it seems to me that
it would only create confusion.

Unconvincingly yours,

--
Arnaud



More information about the Python-3000 mailing list