Are they equivalent?
No. The second one isn't even a generator (no yield expression)
Which one should I use?
The first one is fine. The return statement and also the value None
explicitly yielded are redundant and can be removed (but they're not harming anything either).
def myfunc():
print("doing stuff before yielding the first (and only) value")
yield
print("doing more stuff before raising StopIteration")
Should you use an explicit return statement, then the value returned will appear on the StopIteration
exception instance (literally a value
attribute).
Syntactically, you can put code after the return
statement, but it will be unreachable. So, there's no point to do that.