According to PEP479, the raise of StopIteration to end the loop of a generator may lead to hard to find bugs. This PEP specifies that raise StopIteration has to be replaced by a simple return statement.
def some_generator(n):
for i in range(n):
yield i
if i >= 5:
raise StopIteration # Will raise an exception
def some_generator(n):
for i in range(n):
yield i
if i >= 5:
return # Will close the generator