yield函数生成器

在python中,有yield的函数是生成器函数

def gen(para):
    while True:
        yield para


print(next(gen(8)))
print(next(gen(88)))
print(next(gen(888)))

print("--------------------------------")

def gen2():
    l=["This","is","genernator","!","#"]
    yield l


print(gen2())
print(next(gen2()))


print("--------------------------------")

def gen3():
    l=["This","is","genernator","!","#"]
    yield from l
f=gen3()
while True:
    try:
        print(next(f))
    except StopIteration:
        break

print("--------------------------------")

def gen4():
    while True:
        r=yield
        print(r)


r=gen4()
r.send(None)
r.send("genernator1")
r.send("genernator2")
8
88
888
--------------------------------
<generator object gen2 at 0x019BBF08>
['This', 'is', 'genernator', '!', '#']
--------------------------------
This
is
genernator
!
#
--------------------------------
genernator1
genernator2

 

posted @ 2021-01-01 23:26  腹肌猿  阅读(126)  评论(0编辑  收藏  举报