【python 生成器 面试必备】yield关键字,协程必知必会系列文章--自己控制程序调度,体验做上帝的感觉 3完结

In this article we’ll see how we can close generators and throw exceptions to generators. If you want to know what is a generator, sending values to generator, you can look at part I and part II of the yield series respectively.
这篇文章我们讲学习close 生成器和throw exceptions to generators
ref:https://medium.com/geekculture/yield-python-part-iii-643b7b240f1f

close

In summary, there is close() method that exits generator and puts the generator to a GEN_CLOSED state. This is done by python by handling GeneratorExit exception without a caller’s involvement. Any other kind of exception raised from a generator is seen by a caller. We can also throw exception to a generator. If a generator returns or raises an exception, it will be seen by a caller.
总之,有一个close()方法可以退出生成器并将生成器置于GEN_CLOSED状态。这是由python通过处理GeneratorExit异常而无需调用者参与来完成的。从生成器引发的任何其他类型的异常都由调用者看到。我们还可以向生成器抛出异常。如果生成器返回或引发异常,它将被调用者看到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值