Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:40:44+00:00 2026-06-15T02:40:44+00:00

I am trying to understand some of the subtle details of python generators. One

  • 0

I am trying to understand some of the subtle details of python generators. One of the test programs I wrote to see if I could both send and read alternatively values to/from the same generator is the following:

def injector():
    while True:
        try:
            print 'a'
            v = yield
            print 'b', v
            yield v
            print 'c'
        except GeneratorExit:
            print 'exit'
            break

g = injector()

print 'send none'
g.send(None)
print 'send 2'
g.send(2)
print 'receiving'
v = g.next()
print 'received', v

g.close()

The expected output for this program is:

send none
a
send 2
b 2
receiving
received 2
c
a
exit

The output I get is:

send none
a
send 2
b 2
receiving
c
a
received None
exit

Now, obviously, the question is why am I getting the above output ? What is it that I did not understand about how generators work ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:40:46+00:00Added an answer on June 15, 2026 at 2:40 am

    Let me try to clarify:

    def injector():
        while True:
            try:
                print 'a'
                v = yield
                print 'b', v
                yield v
                print 'c'
            except GeneratorExit:
                print 'exit'
                break
    
    g = injector()
    
    print 'send none'
    g.send(None)
    

    Here the coroutine is started. It executes until the first yield whose result is returned from .send(), but then discarded.

    The coroutine prints a and yields nothing, thus None. So it is ok to discard.

    print 'send 2'
    g.send(2)
    

    Here you send a 2 to the coroutine, making it continue where you left it. v = 2.

    It prints 2 and yields v again. You would expect that from the g.send() call.
    So after receiving and discarding v, you do

    print 'receiving'
    v = g.next()
    

    Here you give control to the coroutine again, which prints c, then a, then yields None again, which you get here.

    print 'received', v
    

    thus prints None for v.

    What you probably want is

    g = injector()
    
    print 'send none'
    g.send(None)
    print 'send 2'
    v = g.send(2)
    print 'received', v
    
    g.close()
    

    (Note that this last block can be written more cleanly and nicely as follows:

    from contextlib import closing
    with closing(injector()) as g:
        print 'send none'
        g.send(None)
        print 'send 2'
        v = g.send(2)
        print 'received', v
    

    )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand some MATLAB source codes. I don't know exactly which one
I'm a beginner iOS/ObjectiveC coder and am trying to understand some details rather than
I'm trying to understand some of Python's built in heap functionality. It seems to
I am trying to understand some really basics of image processing. One of them
I am not a python guy and I am trying to understand some python
I am now trying to understand some code and I have found a pattern,
This might be difficult to answer. I am trying to understand some codebase and
I have some legacy C++ code that I am trying to understand a bit
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I am trying to understand the Javascript event handling. I like some code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.