izip_longest в itertools: что здесь происходит?

Я изо всех сил пытаюсь понять, как работает приведенный ниже код. Это из http://docs.python.org/library/itertools.html#itertools.izip_longest и является эквивалентом итератора izip_longest на чистом питоне. Особенно меня озадачивает функция дозорного, как это работает?

def izip_longest(*args, **kwds):
    # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
        yield counter()         # yields the fillvalue, or raises IndexError
    fillers = repeat(fillvalue)
    iters = [chain(it, sentinel(), fillers) for it in args]
    try:
        for tup in izip(*iters):
            yield tup
    except IndexError:
        pass

7
задан kennytm 14 March 2011 в 12:56
поделиться