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 7196755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:50:19+00:00 2026-05-28T20:50:19+00:00

I am having a bit of trouble with doctest and coroutines… def coroutine(func): def

  • 0

I am having a bit of trouble with doctest and coroutines…

def coroutine(func):
    def start(*args, **kwargs):
        cr=func(*args, **kwargs)
        cr.next()
        return cr
    start.__name__=func.__name__
    return start

@coroutine
def leader_tracking():
    """
    Tracks 'leader' status - only returns transitions

    >>> lt=leader_tracking()
    >>> print lt.send(False)

    >>> print lt.send(False)

    """
    last_status=False
    result=("nop", None)

    while True:
        status=(yield result)

        if status!=last_status:
            direction="up" if last_status==False else "down"
            last_status=status
            result=("tr", direction)
        else:
            result=("nop", None)

If I use the usual doctest scaffolding:

if __name__=="__main__":
    import doctest
    doctest.testmod()

doctest doesn’t show anything whilst if I use a more brute force method:

lt=leader_tracking()
print lt.send(True)
print lt.send(False)
print lt.send(False)
print lt.send(True)
print lt.send(True)
print lt.send(True)
print lt.send(True)
print lt.send(False)

I can see the expected result:

('tr', 'up')
('tr', 'down')
('nop', None)
('tr', 'up')
('nop', None)
('nop', None)
('nop', None)
('tr', 'down')

What am I doing wrong with doctest ?

  • 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-28T20:50:20+00:00Added an answer on May 28, 2026 at 8:50 pm

    The doctest module looks at docstrings which are stored in the __doc__ attribute of a function. Your coroutine decorator only copies __name__, so the docstring gets lost and doctest can’t find it. You could manually assign the __doc__ attribute, but a better way is to use the functools module:

    import functools
    
    def coroutine(func):
        @functools.wraps(func)
        def start(*args, **kwargs):
            cr = func(*args, **kwargs)
            cr.next()
            return cr
        return start
    

    The functools.wraps decorator copies all the various metadata attributes of func into the wrapping function start, including the docstring, so that doctest will work as expected.

    Also note that your doctest comment should include the expected output in order to work properly:

    @coroutine
    def leader_tracking():
        """
        Tracks 'leader' status - only returns transitions
    
        >>> lt=leader_tracking()
        >>> print lt.send(True)
        ('tr', 'up')
        >>> print lt.send(False)
        ('tr', 'down')
        >>> print lt.send(False)
        ('nop', None)
        """
        last_status = False
        result = ("nop", None)
    
        while True:
            status = yield result
    
            if status != last_status:
                direction = "up" if last_status == False else "down"
                last_status = status
                result = ("tr", direction)
            else:
                result = ("nop", None)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of trouble using the List.Find with a custom predicate i have
Having a bit of trouble wrapping my head around this one. I'm sure the
Im having a bit of trouble getting my JSON to be recognised by my
I'm having a bit of trouble loading an html file in a WebView control
I'm having a bit of trouble with a a function that the mongodb c
I'm having a bit of trouble with jQuery Deferred. I'm using a jQuery ajax
I'm having a bit of trouble understanding what the assignment operator is used for
I'm having a bit of trouble understanding how to send a 2D array to
I'm having a bit of trouble with saving the paint strokes in my iOS
I'm having a bit of trouble iterating and retrieving width() values. $(.MyClass).each( function ()

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.