izip_longest in itertools: o que está acontecendo aqui?
Estou tentando entender como o código abaixo funciona. É dehttp: //docs.python.org/library/itertools.html#itertools.izip_longes e é o equivalente em python puro do iterador izip_longest. Estou especialmente intrigado com a função sentinela, como funciona?
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