On Tue, Jul 29, 2008 at 11:06 PM, Raymond Hettinger <[EMAIL PROTECTED]> 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/)
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com