我需要模拟izip_longest从itertools在Python 2.4
import itertools
class Tools:
@staticmethod
def izip_longest(*args, **kwds):
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
fillvalue = kwds.get('fillvalue')
counter = [len(args) - 1]
def sentinel():
if not counter[0]:
raise ZipExhausted
counter[0] -= 1
yield fillvalue
fillers = itertools.repeat(fillvalue)
iterators = [itertools.chain(it, sentinel(), fillers) for it in args]
try:
while iterators:
yield tuple(map(next, iterators))
except ZipExhausted:
pass
class ZipExhausted(Exception):
pass
Run Code Online (Sandbox Code Playgroud)
一切正常,直到我到达yield tuple(map(next, iterators)); Python 2.4抛出一个
NameError: global name 'next' is not …Run Code Online (Sandbox Code Playgroud)