As per PEP255, empty return statement is allowed in the generator function
borrowing the example from pep255
borrowing the example from pep255
def f1(): try: return except: yield 1 print('Loop over empty generator') for item in f1(): print(item) print('Loop over empty generator completed') print('call next() on empty generator') next(f1())
Output:Loop over empty generator
Loop over empty generator completed
call next() on empty generator
Traceback (most recent call last):
File "***", line **, in <module>
next(f1())
StopIteration
or without return
def empty_generator(): for item in []: yield item print('Loop over empty generator') for item in empty_generator(): print(item) print('Loop over empty generator completed') print('call next() on empty generator') next(empty_generator())
Output:Loop over empty generator
Loop over empty generator completed
call next() on empty generator
Traceback (most recent call last):
File "***", line **, in <module>
next(empty_generator())
StopIteration
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs