我们都知道for in
在代码中出现的次数相当的频繁, 那么你知道for in
循环中到底发生了什么吗?
答: 当我们调用一个for x in dataContainer
的时候, 实际上是先调用了dataContainer
的__iter__()
方法来获得它的iterator
(迭代器), 然后不断的调用next()
方法, Python3.x里面是__next__()
, 直到迭代器抛出StopIteration
的异常, 停止
上一篇如何理解yield中已经对interable
已经略有介绍, 我们可以简单的认为可以使用for in
的都是iterable
的, 但是这只是从使用场景来说的, 下面就从它内部来说下
iterable
: 如果一个数据容器定义了一个__iter__()
方法, 那么它就是可迭代的iterator
: 如果一个object
支持迭代协议
, 也就是: 1. 定义一个__iter__
返回它自身 2. 定义一个next()
方法, 每次被调用的时候返回下一个值很明显list
dict
是iterable
的, 但它不是iterator
>>> a = [1,2,3]
>>> a.__iter__
>>> a.next()
Traceback (most recent call last):
File \"\", line 1, in
AttributeError: \'list\' object has no attribute \'next\'
但是通过__iter__
获得的就是它们的iterator
迭代器
>>> ai = a.__iter__()
>>> ai
>>> ai.next()
1
>>> ai.__iter__() is ai
True
class MyList(list):
def __iter__(self):
return MyListIter(self)
class MyListIter(object):
\"\"\"一个实现List的Iterator的 Demo Class\"\"\"
def __init__(self, lst):
self.lst = lst
self.i = -1
def __iter__(self):
return self
def next(self):
if self.i
上面的一段代码中 MyListIter
就实现了 MyList
的迭代器, 运行结果应该是
type(a): , type(ia):
1 2 3 4
比如我们有一个需求, 需要返回所有0~4
中数字和a~e
中字母组合情况, 一般我们可能会这样写
class Combinations:
def __init__(self):
self.combs = []
for x in range(5):
for y in [\'a\', \'b\', \'c\', \'d\', \'e\']:
self.combs.append(\"%s%s\" % (x, y))
for c in Combinations().combs: print c
这样的话, 我们每次都要调用Combinations
的\’combs\’才能拿到所有的组合情况, 显然每次暴露combs
出来非常的不优雅, 为什么不能for c in Combinations()
这样呢?
当然可以, 定义一个__iter__
方法返回combs
的迭代器就可以了
class Combinations:
def __init__(self):
self.combs = []
for x in range(5):
for y in [\'a\', \'b\', \'c\', \'d\', \'e\']:
self.combs.append(\"%s%s\" % (x, y))
def __iter__(self):
return iter(self.combs)
for c in Combinations(): print c
参考: Understanding Python Iterables and Iterators