Why does next raise a 'StopIteration', but 'for' do a normal return?

129,281

The for loop listens for StopIteration explicitly.

The purpose of the for statement is to loop over the sequence provided by an iterator and the exception is used to signal that the iterator is now done; for doesn't catch other exceptions raised by the object being iterated over, just that one.

That's because StopIteration is the normal, expected signal to tell whomever is iterating that there is nothing more to be produced.

A generator function is a special kind of iterator; it indeed raises StopIteration when the function is done (i.e. when it returns, so yes, return None raises StopIteration). It is a requirement of iterators; they must raise StopIteration when they are done; in fact, once a StopIteration has been raised, attempting to get another element from them (through next(), or calling the .next() (py 2) or .__next__() (py 3) method on the iterator) must always raise StopIteration again.

GeneratorExit is an exception to communicate in the other direction. You are explicitly closing a generator with a yield expression, and the way Python communicates that closure to the generator is by raising GeneratorExit inside of that function. You explicitly catch that exception inside of countdown, its purpose is to let a generator clean up resources as needed when closing.

A GeneratorExit is not propagated to the caller; see the generator.close() documentation.

Share:
129,281

Related videos on Youtube

sophros
Author by

sophros

I am passionate for Natural Language Processing, Computational Linguistics, Machine Learning and Artificial Intelligence. On the daily basis I am a Data Scientist using mainly Python (since 2004 and v. 2.3), but I have tried also C++, and .NET while currently making inroads into Julia territories. Recent interests incl. Causation, Document Intelligence, Probabilistic Programming (Gen.jl), Active Learning, Imbalanced Learning, NLG, Formal Concept Analysis. Beyond my technical and research interests, I have seriously romanced with Project Management (PMP since 2011), and Scrum (CSM since 2010). Out of random trivia facts, I am proud to have discovered TRIZ in early '90s while it seems like a buzzword of just the last few years...

Updated on July 08, 2022

Comments

  • sophros
    sophros almost 2 years

    In this piece of code, why does using for result in no StopIteration or is the for loop trapping all exceptions and then silently exiting? In which case, why do we have the extraneous return?? Or is the raise StopIteration caused by: return None?

    #!/usr/bin/python3.1
    def countdown(n):
        print("counting down")
        while n >= 9:
            yield n
            n -= 1
        return
    
    for x in countdown(10):
        print(x)
    
    c = countdown(10)
    next(c)
    next(c)
    next(c)
    

    Assuming StopIteration is being triggered by: return None. When is GeneratorExit generated?

    def countdown(n):
        print("Counting down from %d" % n)
        try:
            while n > 0:
                yield n
                n = n - 1
        except GeneratorExit:
            print("Only made it to %d" % n)
    

    If I manually do a:

    c = countdown(10)
    c.close() #generates GeneratorExit??
    

    In which case why don't I see a traceback?

  • Qiang Xu
    Qiang Xu almost 3 years
    StopIteration could probably be avoided by providing a default value for next(), in case of no match is found, like next(generator_expression, None).