用一幅图来概括:
1.生成器
定义生成器
方式一:
1
2
3
|
/ / 区别于列表生成式 gen = [x * x for x in range ( 5 )] gen = (x * x for x in range ( 5 )) print (gen) / / Out:<generator object <genexpr> at 0x00000258DC5CD8E0 > |
方式二:
1
2
3
4
5
6
7
|
def fib(): prev, curr = 0 , 1 while True : yield curr prev, curr = curr, curr + prev f = fib() print (f) / / Out:<generator object fib at 0x00000258DC5CD150 > |
定义成功后,我们可以利用next()访问生成器下一个元素
1
2
3
4
5
|
print ( next (gen)) / / 0 print ( next (gen)) / / 1 ... print ( next (gen)) / / 16 print ( next (gen)) / / StopIteration |
但一般用for循环遍历
1
2
|
for n in gen: print (n) / / 0 1 4 9 16 |
2.迭代器
任何实现了__iter__和__next__()方法的对象都是迭代器。__iter__返回迭代器自身,__next__返回容器中的下一个值。所以生成器是特殊的迭代器,她内部具有这两种方法。
一个自定义的迭代器如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Fib: def __init__( self ): self .prev = 0 self .curr = 1 def __iter__( self ): return self def __next__( self ): value = self .curr self .curr + = self .prev self .prev = value return value f = Fib() count = 1 for n in f: print (n) count = count + 1 if count> = 10 : break / / Out: 1 1 2 3 5 8 13 21 34 |
3.可迭代对象
像list,tuple,set,dict,str等可以直接作用于for循环的对象,称为可迭代对象。可迭代对象实现了__iter__方法,用于返回迭代器。
1
2
3
4
|
demo = [ 1 , 2 , 3 , 4 ] print ( isinstance (demo, Iterable)) / / True iter_object = iter (demo) print (iter_object) / / <list_iterator object at 0x00000258DC5EF748 > |
原文链接:https://segmentfault.com/a/1190000013121618