Estoy luchando por comprender cómo funciona el siguiente código. Es de http://docs.python.org/library/itertools.html#itertools.izip_longest, y es el equivalente de python puro del iterador izip_longest. Estoy especialmente desconcertado por la función centinela, ¿cómo funciona?izip_longest en itertools: ¿Qué está pasando aquí?
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
Además, 'sentinel()' se puede llamar un número infinito de veces sin ninguna excepción planteada. –
@Sven: Tienes razón, lo arreglé. –