Within a generator function, how can I find out if it has yielded anything yet?
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.'
You’d need to keep a flag yourself, or restructure the code at the top. If things are too complex, it sounds like your function might do too much.
Also, if that’s your message, it sounds like you may want an exception instead.