[Python-ideas] Positional only arguments

Steven Bethard steven.bethard at gmail.com
Sat May 19 06:15:22 CEST 2007


On 5/18/07, Aaron Brady <castironpi at comcast.net> wrote:
> Steven Bethard wrote:
> > On 5/18/07, Jim Jewett <jimjjewett at gmail.com> wrote:
> > > How could there ever be an argument that *needs* to be positional?
> >
> > As I've mentioned before, a great example of this is the dict()
> > signature. It takes an optional "container" argument, followed by
> > arbitrary keywords.  If you write this in the naive way::
> >
> >     def __init__(self, container=None, **kwargs)
> >         ...
> >         for key, value in kwargs.items():
> >             self[key] = value
> >
> > then you get TypeErrors for the following code::
> >
> >     d.update(sequence=1, container=2)
> >     d.update(other=1, self=2)
> >
> > That is, the **kwargs can never include anything named 'self' or
> > 'container'. If you could declare these two arguments as
> > positional-only, you'd never run into this problem.
>
> Might you use d.update(container=2, sequence=1)?  'Can never' caught me up.

Not sure I understand the question. My point was only that you'll
never reproduce this behavior::

    >>> dict(self=1, other=2)
    {'self': 1, 'other': 2}
    >>> d = {}
    >>> d.update(container=2, sequence=1)
    >>> d
    {'container': 2, 'sequence': 1}

Of course, you can rename the 'self' and 'container' parameters to
something else, but that just means that you can't use whatever new
names you choose. The only way to solve this is to use real
positional-only arguments, which currently means using *args and
parsing out things within the function body.

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