[Python-3000] Is this really a SyntaxError?

Raymond Hettinger python at rcn.com
Wed Jul 30 08:06:45 CEST 2008


>>   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.

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

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


Raymond


More information about the Python-3000 mailing list