Optional yield or return in python3. How to?
问题 I would like to have a function that can, optionally, return or yield the result. Here is an example. def f(option=True): ... for...: if option: yield result else: results.append(result) if not option: return results Of course, this doesn't work, I have tried with python3 and I always get a generator no matter what option value I set. As far I have understood, python checks the body of the function and if a yield is present, then the result will be a generator. Is there any way to get around