Yield after the return in Python function.

Chris Angelico rosuav at gmail.com
Mon Apr 5 13:53:56 EDT 2021


On Tue, Apr 6, 2021 at 3:46 AM Terry Reedy <tjreedy at udel.edu> wrote:
> *While 'a and not a' == False in logic, in Python it might raise
> NameError.  But that would still mean that it is never True, making
> 'yield 0' still unreachable.
>

And even just the lookup can have side effects, if your code is
pathologically stupid.

>>> class Wat(dict):
...     def __missing__(self, key):
...             global count
...             count -= 1
...             return count
...
>>> count = 2
>>> eval("print(a and not a)", Wat(print=print))
True

So Python can't afford to treat this as dead code.

ChrisA


More information about the Python-list mailing list