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

 

上一篇:推荐一款 SpringBoot + Mybatis + Vue的代码生成器


下一篇:生成器