问题描述
是否有一种简单的方法来测试生成器是否没有类似这些项目的项目,例如peek
,hasNext
,isEmpty
?
Is there a simple way of testing if the generator has no items, like peek
, hasNext
, isEmpty
, something along those lines?
推荐答案
您问题的简单答案:不,没有简单的方法.有很多解决方法.
The simple answer to your question: no, there is no simple way. There are a whole lot of work-arounds.
实际上不应该有一种简单的方法,因为生成器是什么:一种在不将序列保存在内存中的情况下输出值序列的方法.因此没有向后遍历.
There really shouldn't be a simple way, because of what generators are: a way to output a sequence of values without holding the sequence in memory. So there's no backward traversal.
如果愿意,您可以编写has_next函数,甚至可以将其作为带有精美装饰器的方法添加到生成器上.
You could write a has_next function or maybe even slap it on to a generator as a method with a fancy decorator if you wanted to.
这篇关于我怎么知道一开始发电机是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!