[Python-ideas] Positional only arguments

Steven Bethard steven.bethard at gmail.com
Mon May 21 08:58:43 CEST 2007


On 5/21/07, Adam Olsen <rhamph at gmail.com> wrote:
> On 5/20/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> > No. My current proposal is:
> >
> > - Everything after a * is a positional-only argument, up to the first **
> > - Everything after a ** is a keyword-only argument
> > - A lone * may only appear at the beginning of the signature
> >
> > In essence, this is changing the lone * of `PEP 3102`_ into a lone **,
> > and allowing a lone * at the beginning of the signature to indicate
> > that all positional arguments are positional-only.
> >
> > .. _PEP 3102: http://www.python.org/dev/peps/pep-3102/
> >
> > That means that in the following signature::
> >
> >     def f(*, a, b=None, *args, **, c=42, **kwargs)
> >
> > - 'a' and 'b' are positional-only
> > - 'args' is positional-only (as usual)
> > - 'c' is keyword-only
> > - 'kwargs' is keyword-only (as usual)
> >
> > If you want positional-or-keyword arguments, simply omit the initial *::
> >
> >     def f(a, b=None, *args, **, c=42, **kwargs)
>
> ** is redundant here.  You get the same behaviour without it:
>
>     def f(a, b=None, *args, c=42, **kwargs)
>
> See below though.

I note that below you argue for explicit is better than implicit.
Requiring the ** even when it could be inferred from the *args is a
good example of EIBTI.  I don't have strong feelings on this issue
though.

> I initially had the same reservations you do about my proposal, but
> now I think there's a bigger problem: it undermines the obviousness of
> PEP 3102's behaviour.
[snip]
>
>     def compare(a, b, *, key=None):
>
> You continue sucking up extra positional args, but since it's nameless
> there must be none of them.  Again, obvious.

I have to disagree. The first thing I think when I see a lone * is
multiplication, not "keyword-only arguments start after me". I would
say it's just as obvious to see::

    def g(*, a, b)

and say, "oh, it's after a * so it must be positional-only just like
*args is". That is, I don't think the PEP 3102 meaning for lone * is
any more intuitive than the meaning I'm proposing here.

> Where as PEP 3102 is motivated by "explicit is better than implicit",
> positional-only arguments actually *want* implicit!

I don't follow this logic. How is a positional-only argument more
implicit?  You still have to supply it on every function call.  It's
not like you can omit it.  Or are you saying that using positional
arguments instead of keyword arguments is implicit?

> A decorator may be okay, but I'm unsure the
> use cases are significant enough to warrant adding it.

Just for reference, here are the current motivating use cases:

* User mapping types that need to match the dict() and dict.update()
method signatures
* Code where positional argument names might change, e.g. George's def
f(sequence) which later becomes def f(iterable)

Particularly since user mappings are relatively common, I do think
this issue deserves at least a decorator.

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy



More information about the Python-ideas mailing list