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

  • Home
  • SEARCH
  • 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 3605282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:06:15+00:00 2026-05-18T21:06:15+00:00

Writing a class, how do I implement foo.send(item) ? __iter__ allows iterating over the

  • 0

Writing a class, how do I implement

foo.send(item) ?

__iter__ allows iterating over the class like a generator, what if I want it to be a coroutine?

  • 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-05-18T21:06:15+00:00Added an answer on May 18, 2026 at 9:06 pm

    Here is a basic example of a coroutine:

    def coroutine(func):
        def start(*args,**kwargs):
            cr = func(*args,**kwargs)
            cr.next()
            return cr
        return start
    
    @coroutine
    def grep(pattern):
        print "Looking for %s" % pattern
        while True:
            line = (yield)
            if pattern in line:
                print(line)
    
    g = grep("python")
    # Notice how you don't need a next() call here
    g.send("Yeah, but no, but yeah, but no")
    g.send("A series of tubes")
    g.send("python generators rock!")
    # Looking for python
    # python generators rock!
    

    We can make a class which contains such a coroutine, and delegates calls to its send method to the coroutine:

    class Foo(object):
        def __init__(self,pattern):
            self.count=1
            self.pattern=pattern
            self.grep=self._grep()
        @coroutine
        def _grep(self):
            while True:
                line = (yield)
                if self.pattern in line:
                    print(self.count, line)
                    self.count+=1
        def send(self,arg):
            self.grep.send(arg)
    
    foo = Foo("python")
    foo.send("Yeah, but no, but yeah, but no")
    foo.send("A series of tubes")
    foo.send("python generators rock!")
    foo.pattern='spam'
    foo.send("Some cheese?")
    foo.send("More spam?")
    
    # (1, 'python generators rock!')
    # (2, 'More spam?')
    

    Notice that foo acts like a coroutine (insofar as it has a send method), but is a class — it can have attributes and methods which can interact with the coroutine.

    For more information (and wonderful examples), see David Beazley’s Curious Course on Coroutines and Concurrency.

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

Sidebar

Related Questions

I'm currently trying to implement a StopWatch class. The interface is something like: interface
I'm currently writing a class that implements the SeekableIterator interface and have run into
When writing a class do you group members variables of the same type together?
I'm busy writing a class that monitors the status of RAS connections. I need
I'm currently writing a class to handle all database-activity in my application, and so
When writing an abstract class, or a class that doesn't get instantiated directly... do
I'm writing a JavaSCript class that has a method that recursively calls itself. Scheduler.prototype.updateTimer
I'm currently writing a C#-class in my ASP.NET (3.5) application to handle all database-queries.
I was writing a database handler class in PHP using the mysqli class and
I'm writing some code for a class constructor which loops through all the properties

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.