[Python-ideas] Positional only arguments

Jim Jewett jimjjewett at gmail.com
Fri May 18 23:52:01 CEST 2007


On 5/16/07, George Sakkis <george.sakkis at gmail.com> wrote:
> 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.

uhh... could you spell those out, please?

keywords-only is justified by functions that take both *args and a
keyword that meets meets at least one of

    (1)  Wasn't there before, so you can't stick it in front for
backwards-compatibility reasons.  (think "cmp")

    (2)  Doesn't have a default

    (3)  Should always be called by name, for readability.

The following signature is awful

    def max(cmp=mysort, *args):

And if you've already released max, it isn't even legal.


How could there ever be an argument that *needs* to be positional?

There are some that don't work if you call them by keyword, because
they happen to be implemented in C without keyword support, but ... is
there any reason to postively forbid using the argument name?  The
closest I can come to an example is

    def lappend(object):
        # This mimics list.append, and the *real* function wouldn't
        # take a keyword, so neither will I, just to prevent bad habits.

Even that can already be written if you really want to...

    def lappend(*object):
        if len(object) != 1: raise TypeError("lappend takes ...")
        object = object[0]
        ...

-jJ



More information about the Python-ideas mailing list