Created: 2026-05-29 09:13 by Thom
In Python there are some weird stuff happening with the `return` statement. Look at this: In [1]: def f1(): ...: return None ...: In [2]: def f2(): ...: return None ...: yield ...: In [3]: print(f1()) None In [4]: print(f2()) Why?? Why does this happen? Well I found something. It is all to do with flags (CO_GENERATOR) in this case. In [6]: import dis In [7]: f2.__code__.co_flags & 0x20 Out[7]: 32 In [8]: f1.__code__.co_flags & 0x20 Out[8]: 0 In [9]: But this doesn't make the `return` meaningless. In [9]: gen = f2() In [10]: next(gen) --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) Cell In[10], line 1 ----> 1 next(gen) StopIteration: So Python does some interesting stuff at compile time to do generators. And they need to be like this because they are lazy.