(original) (raw)
On 3/1/07, Raymond Hettinger <raymond.hettinger@verizon.net> wrote:
Nope. You can only have one one-star and one two-star. If you want keyword-only arguments \*and\* arbitrary positional arguments, you can just do it (in Py3k, and it will probably be backported to 2.6.)
>>> def foo(a, b, \*args, c=1): return a, b, args, c
...
>>> foo(1, 2)
(1, 2, (), 1)
>>> foo(1, 2, 3, 4, c=5)
(1, 2, (3, 4), 5)
The one-star-only syntax is only if you \*don't\* want arbitrary positional arguments:
What we have now:
def f(a, b, *args, **dict(c=1)): # Yuck!
What we really need:
def f(a, b, *args, **, c=1): # Two stars spell dictionary.
What I heard was planned instead:
def f(a, b, *args, *, c=1): # One star spells iterable.
Nope. You can only have one one-star and one two-star. If you want keyword-only arguments \*and\* arbitrary positional arguments, you can just do it (in Py3k, and it will probably be backported to 2.6.)
>>> def foo(a, b, \*args, c=1): return a, b, args, c
...
>>> foo(1, 2)
(1, 2, (), 1)
>>> foo(1, 2, 3, 4, c=5)
(1, 2, (3, 4), 5)
>>> def foo(a, b, \*, c=1): return a, b, c
...
>>> foo(1, 2)
(1, 2, 1)
>>> foo(1, 2, 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 2 positional arguments (3 given)
>>> foo(1, 2, c=5)
(1, 2, 5)
--
Thomas Wouters <thomas@python.org>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!