[Python-ideas] Positional only arguments

George Sakkis gsakkis at rutgers.edu
Fri May 18 03:14:57 CEST 2007


On 5/17/07, Giovanni Bajo <rasky at develer.com> wrote:

> On 16/05/2007 16.50, George Sakkis wrote:
>
> > In short, PEP 3102 enables the declaration of keyword-only arguments,
> > making it easier for an API to grow in a backwards compatible way. As
> > Benji York, I see the utility of positional-only arguments, not just
> > for the sake of symmetry but for pragmatic reasons; in fact the same
> > reasons that serve as rationale to the keywords-only PEP.
>
> I've needed this only a few times, and I have used something like:
>
> def foo(*args):
>     a,b,c = args
>
> as a workaround. Doesn't look too clumsy after all.

Well, it's better than nothing but it's an abuse of the *varargs
syntax and not friendly to signature inspection tools.

> For Py4k, the only thing that occurred to me is to reuse the parenthesis:
>
> def foo((a, b, c, d), e, f, *, g, h):
>     pass
>
> - a,b,c,d are positional-only
> - e,f can be either positional or keyword
> - g,h are only keyword.

Not too bad, but the pair of parentheses is redundant; positional-only
args (if any) must start from index 0. Also this doesn't address
pos-only args with defaults like Guido's example foo(abc, xyz=42). A
single "delimiter" symbol (e.g. '%') between pos-only and pos-or-kw
would kill both birds with one stone, i.e. something like:

def foo(a, b=42,           # pos-only
             % ,                   # delimiter
             c=0, d=None, # pos-or-kw
             *args,              # varargs
             e=-1,z='',        # kw-only
             **kwds            # keyword dictionary
)

Pros:
- Backwards compatible: args are pos-or-kw by default as they are now;
only those before the new delimiter are positional.
- Easy to change the status of a parameter; just move it to the
appropriate section (at least if it has a default value).

Cons:
- Yet another magic symbol, smells like perl (reusing the single star
would be ambiguous).
- The presumably common cases of functions without pos-or-kw args is clumsy:
def foo(a, b, c=31, %):  # all pos-only
def bar(a, b=1, %, *, c=31, d=None):  # pos-only or kw-only

Overall, I still prefer the double underscores.

George

-- 
"If I have been able to see further, it was only because I stood on
the shoulders of million monkeys."



More information about the Python-ideas mailing list