python - 生成器表达式中的 close()
问题描述
close
在使用生成器/生成器表达式时曾经使用过,还是主要是内部方法?例如:
def odd_less_than_six():
n = 1
while n < 6:
yield n
n += 2
...
>>> f=odd_less_than_six()
>>> next(f)
1
>>> next(f)
3
>>> next(f)
5
>>> next(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
或手动:
>>> f=odd_less_than_six()
>>> next(f)
1
>>> f.close()
>>> next(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
close
在生成器方法中何时手动调用是否有真实的用例?
解决方案
推荐阅读
- android - 部分分页
- go - 如何使用 VSCode 远程调试 Go 代码
- c# - c#同时将多个对象插入数据库
- javascript - 如何将动态参数传递给 setInterval 函数?
- php - 如何查询最后一个数据,但对最后一个数据的第一次查询除外
- github - VSCode 连接到 GitHub 时出现 SSL 错误
- java - Hibernate ElementCollection/JoinTable IntegrityConstraintViolationException
- magento - Magento 在哪里存储交叉引用数据
- django - 如何修复 GeoDjango OSError:未定义符号?
- java - 如何在一周中的特定日期和时间分别创建多个预定通知