I cannot figure why the behavior of __iter__ changes when I uncomment if False: yield. The condition is never true, so why does the result change?
class Example:
def __init__(self):
self.lst = [1,2,3]
self.size = 3
def __iter__(self):
self.n = 0
#if False: yield
return self
def __next__(self):
self.n += 1
if self.n > self.size:
raise StopIteration
return self.lst[self.n-1]
ex = Example()
for i in ex:
print(i)
In this code everything works as expected, and it prints out the list.
If I uncomment the the if False: yield in the __iter__ method then the iterator stops working and nothing gets printed out, even if that line never gets executed.