yield函数
在yield嵌套函数中,经常用到yield from 语法糖
案例
有下面案例中可知,若没有yield from时,还得使用for循环进行迭代,有yield from时无需采用for循环
# -*- coding: UTF-8 -*-
from collections import Iterator
def test():
"""
函数:生成特殊的数字
"""
num = map(lambda x:x*10+2, [1,2,3,4,5])
for x in num:
yield (x)
def numout():
"""
函数:从指定数字集合中取数字,并对此数字进行处理
"""
# for y in test():
# yield y
#采用yield from代替for循环
yield from test()
lst = [x for x in numout()]
print(lst)
简单案例
def ceshi():
yield from [1,2,3]
for x in ceshi():
print(x)
运行结果:
1
2
3