0
点赞
收藏
分享

微信扫一扫

python 中yield

十里一走马 2022-10-17 阅读 229


1、可以简单理解为return

2、函数中有yield关键字,函数并不会真的执行,而是先得到一个生成器g(相当于一个对象)

3、直到调用next方法,函数正式开始执行

import time


def foo():
time.sleep(3)
yield 2000


def co():
res = None
f = foo()

try:
while True:
res = next(f)
except StopIteration:
return res


if __name__ == '__main__':
print(co())


举报

相关推荐

0 条评论