izip_longest en itertools: ¿qué está pasando aquí?
Me cuesta entender cómo funciona el siguiente código. Es dehttp: //docs.python.org/library/itertools.html#itertools.izip_longes, y es el equivalente de Python puro del iterador izip_longest. Estoy especialmente desconcertado por la función centinela, ¿cómo 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