Python生成器详解
生成器是一种特殊的迭代器,其创建方式比自定义迭代器类更为简洁
通过括号构造生成器
将列表推导式的方括号替换为圆括号即可创建生成器对象
In [15]: result_list = [x*2 for x in range(5)]
In [16]: result_list
Out[16]: [0, 2, 4, 6, 8]
In [17]: gen_expr = (x*2 for x in range(5))
In [18]: gen_expr
Out[18]: <generator object <genexpr> at 0x7f626c132db0>
该生成器实例可通过next()函数、for循环或list()方法进行迭代,遍历结束后再次调用next()会触发StopIteration异常
In [19]: next(gen_expr)
Out[19]: 0
In [20]: next(gen_expr)
Out[20]: 2
In [21]: next(gen_expr)
Out[21]: 4
In [22]: next(gen_expr)
Out[22]: 6
In [23]: next(gen_expr)
Out[23]: 8
In [24]: next(gen_expr)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-24-380e167d6934> in <module>()
----> 1 next(gen_expr)
StopIteration:
通过函数配合yield实现复杂逻辑
当需要处理复杂算法时,可采用包含yield语句的函数实现生成器。此类函数在调用时不会立即执行,而是返回生成器对象。以下示例展示斐波那契数列生成器的实现
def generate_sequence(limit):
first, second = 0, 1
count = 0
while count < limit:
yield first
first, second = second, first + second
count += 1
return '处理完成'
def main():
seq1 = generate_sequence(10)
seq2 = generate_sequence(10)
seq3 = generate_sequence(5)
# 使用for循环遍历
for num in seq1:
print(num)
# 转换为列表
print(list(seq2))
# 使用while循环处理
while True:
try:
print(next(seq3))
except StopIteration as e:
# 异常对象的value属性包含返回值
print(e.value)
break
if __name__ == '__main__':
main()
关键特性说明:
- 包含yield语句的函数即为生成器,执行时会暂停并保存当前状态
- yield语句具有两个功能:1) 挂起函数执行 2) 返回指定值
- 通过next()方法可恢复生成器执行
- Python3支持生成器返回值,而Python2仅允许return退出但不能带返回值
通过send方法传递参数
除了next()方法,还可使用send()方法唤醒生成器并传递数据。此方法可在恢复执行时注入额外参数,以下示例演示如何通过send设置起始值
def sequence_generator(limit):
first, second = 0, 1
count = 0
while count < limit:
# 接收send传入的参数,next()调用时参数为None
input_value = yield first
print('接收到:', input_value)
# 当接收到有效值时,更新起始参数
if input_value:
second = input_value
first, second = second, first + second
count += 1
def main():
seq = sequence_generator(10)
print(next(seq))
print(next(seq))
# send方法可传递参数,相当于next(seq)等价于seq.send(None)
print(seq.send(None))
# 传递4作为新起始值
print(seq.send(4))
print(seq.send(None))
print(seq.send(None))
if __name__ == '__main__':
main()
运行结果展示参数传递效果