[Python-3000] Is this really a SyntaxError?

Guido van Rossum guido at python.org
Wed Jul 30 21:33:26 CEST 2008

On Tue, Jul 29, 2008 at 11:06 PM, Raymond Hettinger <python at rcn.com> wrote:
>>>  def grouper(n, iterable, fillvalue=None):
>>>      "grouper(3, 'abcdefg', 'x') --> abc def gxx"
>>>      args = [iter(iterable)] * n
>>>      kwds = dict(fillvalue=fillvalue)
>>>      return izip_longest(*args, **kwds)
> [GvR]
>> If you reverse the two parts it will work:
>>  izip_longest(fillvalue=fillvalue, *args)
> Wow, I'm astonished that that works.  How weird.

Long ago this was this simplest solution.

> Am I the only one who didn't know you could
> put keyword arguments before star-args?

As I alluded, I forget this on a regular basis myself.

It's not "could" though. It's "must". :-)

> It's especially odd given that keyword arguments
> are prohibited from preceding positional arguments, so
> you can't just take the star-args version and
> substitute the unpacked values:
> IDLE 2.6b2
>>>> from itertools import izip_longest
>>>> args = 'abcdef', 'AB'
>>>> list(izip_longest(fillvalue='x', *args))
> [('a', 'A'), ('b', 'B'), ('c', 'x'), ('d', 'x'), ('e', 'x'), ('f', 'x')]
>>>> list(izip_longest(fillvalue='x', 'abcdef', 'AB'))
> SyntaxError: non-keyword arg after keyword arg

Yeah, that's why we all keep forgetting it.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list