Python 2.7:"有什么东西已经屈服了吗?"

Jor*_*dan 1 python yield generator

在生成器函数中,如何确定它是否已经产生了什么?

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.'
Run Code Online (Sandbox Code Playgroud)

Jul*_*ian 8

您需要自己保留一个标志,或者重新调整顶部的代码.如果事情太复杂,听起来你的功能可能会做得太多.

此外,如果这是你的消息,听起来你可能想要一个例外.