部分转自https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317799226173f45ce40636141b6abc8424e12b5fb27000:
一,生成器:
1、 #g 相当于__iter__返回的对象,可调用next(g) 拿到下一个值
>>> g = (x * x for x in range(10))
>>> for n in g:
... print(n)
2、g = fib(6)生成一个generator对象<generator object fib at 0x104feaaa0>,然后可调用next(g),遇到yield返回,下次从上次返回的yield处开始执行
>>> for n in fib(6):
... print(n)
def fib(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a + b
n = n + 1
return 'done'
>>> g = fib(6)
>>> while True:
... try:
... x = next(g)
... print('g:', x)
... except StopIteration as e:
... print('Generator return value:', e.value)
... break
...
g: 1
g: 1
g: 2
g: 3
g: 5
g: 8
Generator return value: done
>>> g = (x*x for x in range(10))
>>> g
<generator object <genexpr> at 0x02A37D50>
>>> def fib(max):
a, b = 1, 1
n = 0
while n < max:
yield a
a, b = b, a+b
n = n+1
>>> fib
<function fib at 0x02A3F6A8>
>>> g = fib(6)
>>> g
<generator object fib at 0x02A37D80>
>>> for n in g:
print(n)
1
1
2
3
5
8
>>> g = fib(6)
>>> while True:
try:
n = next(g)
print(n)
except StopIteration as e:
print('Generator return value:', e.value)
break
1
1
2
3
5
8
Generator return value: None
二,
__iter__ 类
如果一个类想被用于for ... in
循环,类似list或tuple那样,就必须实现一个__iter__()
方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的__next__()
方法拿到循环的下一个值,直到遇到StopIteration
错误时退出循环。
我们以斐波那契数列为例,写一个Fib类,可以作用于for循环:
class Fib(object):
def __init__(self):
self.a, self.b = 0, 1 # 初始化两个计数器a,b
def __iter__(self):
return self # 实例本身就是迭代对象,故返回自己
def __next__(self):
self.a, self.b = self.b, self.a + self.b # 计算下一个值
if self.a > 100000: # 退出循环的条件
raise StopIteration()
return self.a # 返回下一个值
现在,试试把Fib实例作用于for循环:
>>> for n in Fib():
... print(n)
...
1
1
2
3
5
...
46368
75025
>>> class Fib(object):
def __init__(self):
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
self.a, self.b = self.b, self.a+self.b
if self.a > 100:
raise StopIteration
return self.a
>>> Fib
<class '__main__.Fib'>
>>> g = Fib()
>>> g
<__main__.Fib object at 0x02A38E70>
>>> next(g)
1
>>> for n in g:
print(n)
1
2
3
5
8
13
21
34
55
89