[Python-Dev] TypeError: f() missing 1 required positional argument: 'x'
Oscar Benjamin
oscar.j.benjamin at gmail.com
Thu Sep 20 17:53:33 CEST 2012
On 20 September 2012 16:14, Benjamin Peterson <benjamin at python.org> wrote:
> 2012/9/20 Mark Dickinson <dickinsm at gmail.com>:
> > And excepting optional ones, too, right? E.g., the c in
> >
> > def foo(a, b, c=1, *args, d):
> > pass
> >
> > can be passed to by position, but isn't "positional".
>
> Why not?
>
> >>> def f(a, b, c=3): pass
> ...
> >>> f()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: f() missing 2 required positional arguments: 'a' and 'b'
> >>> f(1, 2, 3, 4)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: f() takes from 2 to 3 positional arguments but 4 were given
>
The difference between c and a,b is that c is optional, whereas a and b are
required.
In Python 2.x there are named arguments and variadic arguments. There are
two types of named arguments: required and optional. There are also two
types of variadic arguments: positional and keyword. i.e.:
named
required
not-required
variadic
positional
keyword
In Python 2.x all named parameters can be passed by position or by keyword,
so it doesn't make sense to use those concepts to distinguish them. On the
other hand, for variadic parameters that distinction is crucial.
In Python 3.x there are two orthogonal properties for each named parameter.
The parameter can be required or optional as before, and then the parameter
can be keyword-only or positional. There are 4 combinations of these two
properties:
def f(a, b=1, *, c, d=3): pass
| required | optional
positional | a | b
kwonly | c | d
Since there are two orthogonal properties of a parameter (requiredness and
positionness) it makes perfect sense to use two adjectives to describe each
parameter as is the case for the error message shown at the start of this
thread:
Mark Dickinson wrote:
>>>> def f(x): pass
>...
>>>> f()
>Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
>TypeError: f() missing 1 required positional argument: 'x'
I would say that the only problem with this terminology is that it would be
good to think of a word to replace "keyword-only" (positionless?).
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120920/fb93c5ca/attachment-0001.html>
More information about the Python-Dev
mailing list