Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Generator Control with send(), throw(), and close()

Learn how send(), throw(), and close() explicitly control a suspended synchronous generator, including sent values, injected exceptions, propagation, and cleanup.

Levels: Advanced ~24 min read

Advanced

A generator is normally advanced by next() or a for loop. Python also lets the caller control a paused generator explicitly: send() places a value into the paused yield expression, throw() raises an exception there, and close() asks the generator to finish.

Start before sending a value

A newly created generator has not reached a yield expression yet. Its first send() must pass None because there is no paused yield ready to receive another value. The usual equivalent is next(generator).

Interactive Runnable example
Edit

Ready to run.

Send a value into a paused yield

When a generator is paused at value = yield the next send(value) resumes it and makes the current yield expression evaluate to that value. The generator then continues until it yields again or finishes.

Interactive Runnable example
Edit

Ready to run.

next() and send(None) resume in the same way

Both next(generator) and send(None) resume a suspended generator without supplying a useful value to its current yield expression. Use send(value) when the generator is designed to receive information from its caller.

Interactive Runnable example
Edit

Ready to run.

Inject an exception with throw()

throw(exception) resumes the generator by raising that exception at the paused yield. The generator can catch it with ordinary try / except logic and choose what to do next.

Interactive Runnable example
Edit

Ready to run.

An uncaught thrown exception reaches the caller

If the generator does not catch the exception introduced by throw() then the exception leaves the generator and is raised to its caller. throw() changes where the exception is raised; it does not create a second exception-handling system.

Interactive Runnable example
Edit

Ready to run.

close() asks a suspended generator to finish

close() asks a suspended generator to terminate. A finally block still runs during that termination, which makes it a natural place for cleanup when the caller stops iterating early.

Interactive Runnable example
Edit

Ready to run.

Control methods operate on a suspended generator

The control methods in this lesson are operations on generator state. A generator method cannot safely re-enter the same generator while it is already running; Python raises ValueError for that case. In normal code, make each control call only after the previous call has returned or raised.

Interactive Runnable example
Edit

Ready to run.

Quick check

Quick check

Which call supplies a value to the currently paused yield expression?

def worker():
    received = yield "ready"

generator = worker()
next(generator)
generator.("yes")
Was this page helpful?

Mark this learning resource complete to track your learning progress.