除了内置的迭代器类型,任何一个含有__iter__函数、next(Python2)或者__next__函数的类都是迭代器类型。__iter__函数返回一个可迭代的对象;next(Python2)或者__next__函数返回下一个值。
例如,编写一个整数迭代器,从1迭代到100。
# coding=utf8
class IntegerIterator:
def __init__(self):
self._num = 0
def __iter__(self):
return self
def next(self):
if self._num == 100:
raise StopIteration
self._num += 1
return self._num
if __name__ == '__main__':
for a in IntegerIterator():
print(a)
# coding=utf8
class IntegerIterator:
def __init__(self):
self._num = 0
def __iter__(self):
return self
def __next__(self):
if self._num == 100:
raise StopIteration
self._num += 1
return self._num
if __name__ == '__main__':
for a in IntegerIterator():
print(a)
和Python2相比,next函数变成了__next__函数。
要编写兼容Python2和Python3的迭代器,就很简单:
# coding=utf8
class IntegerIterator:
def __init__(self):
self._num = 0
def __iter__(self):
return self
def __next__(self):
if self._num == 100:
raise StopIteration
self._num += 1
return self._num
next = __next__
if __name__ == '__main__':
for a in IntegerIterator():
print(a)
定义__next__
函数,再定义next
指向__next__
。这样的代码就不会报错误:
# Python2
TypeError: instance has no next() method
# Python3
TypeError: iter() returned non-iterator of type '***'