[Python-ideas] Positional only arguments

Steven Bethard steven.bethard at gmail.com
Wed May 16 19:17:30 CEST 2007


On 5/16/07, Guido van Rossum <guido at python.org> wrote:
> But I'd love to see better syntax for positional-only arguments. What
> I would like to be able to do is somehow write a signature like this:
>
>   def foo(abc, xyz=42): ...
>
> where both arguments (if present) *must* be given using positional
> notation, so that foo(42) and foo(42, 24) are legal, but foo(abc=42)
> and foo(42, xyz=24) are illegal. (Alas, I don't have any clever
> suggestions.)

I'd particularly like to see some way to do this because it would make
duplicating the dict() and dict.update() signatures easier.  Currently
you could write::

    class C(object):
        def __init__(self, obj=None, **kwargs):

but this breaks when you try something like::

    C(obj='foo', bar='baz')

and expect both keyword arguments to be caught in the **kwargs.  And
let's hope you never need to write something like::

    C(self=1, other=2)

where you'll get a::

    TypeError: __init__() got multiple values for keyword argument 'self'

The current correct version looks like::

    class C(object):
        def __init__(*args, **kwargs):
            self = args[0]
            if len(args) == 2:
                obj = args[1]
            elif len(args) == 1:
                obj = None
            else:
                raise TypeError()

Nasty.


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