Skip to main content
 首页 » 编程设计

python 2.7 : "Has anything been yielded"

2025年05月04日10Free-Thinker

在生成器函数中,我如何才能知道它是否产生了任何东西?

def my_generator(stuff): 
    # complex logic that interprets stuff and may or may not yield anything 
 
    # I only want to yield this if nothing has been yielded yet. 
    yield 'Nothing could be done with the input.' 

请您参考如下方法:

你需要自己保留一个标志,或者在顶部重构代码。如果事情太复杂,听起来你的函数可能做的太多了。

此外,如果这是您的消息,听起来您可能想要一个异常(exception)。