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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:59:01+00:00 2026-06-12T10:59:01+00:00

I have the following function which I do unit testing with doctest. from collections

  • 0

I have the following function which I do unit testing with doctest.

from collections import deque

def fill_q(histq=deque([])):
    """
    >>> fill_q()
    deque([1, 2, 3])
    >>> fill_q()
    deque([1, 2, 3])
    """
    if histq:
        assert(len(histq) == 0)
    histq.append(1)
    histq.append(2)
    histq.append(3)
    return histq

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

the first case passes, but the second call to fill_q fails, yet it’s the same code:

**********************************************************************
File "trial.py", line 7, in __main__.fill_q
Failed example:
    fill_q()
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.fill_q[1]>", line 1, in <module>
        fill_q()
      File "trial.py", line 11, in fill_q
        assert(len(histq) == 0)
    AssertionError
**********************************************************************
1 items had failures:
   1 of   2 in __main__.fill_q
***Test Failed*** 1 failures.

It looks like that doctest re-uses the local variable histq from the first test call, why is it doing this? This is very silly behaviour (provided it’s not me doing sth crazy here).

  • 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-12T10:59:02+00:00Added an answer on June 12, 2026 at 10:59 am

    The problem is not with doctest, but the default parameter you are using in def fill_q(histq=deque([])). It is similar to this:

    >>> from collections import deque
    >>> 
    >>> def fill_q(data=deque([])):
    ...     data.append(1)
    ...     return data
    ... 
    >>> fill_q()
    deque([1])
    >>> fill_q()
    deque([1, 1])
    >>> fill_q()
    deque([1, 1, 1])
    

    This seemingly odd behaviour happens when you use a mutable object as a default value like a list or a dictionary. It is in fact using the same object:

    >>> id(fill_q())
    4485636624
    >>> id(fill_q())
    4485636624
    >>> id(fill_q())
    4485636624
    

    Why?

    Default parameter values are always evaluated when and only when the def statement they belong to is executed [ref].


    How to avoid this mistake:

    Use None as default parameter instead, or for arbitrary object:

    my_obj = object()
    def sample_func(value=my_obj):
        if value is my_obj:
            value = expression
        # then modify value 
    

    When to use it?:

    1. local rebinding of global names:

      import math
      
      def fast_func(sin=math.sin, cos=math.cos):
      
    2. can be used for memoization (e.g., make certain recursions run faster)

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

Sidebar

Related Questions

I have the following function which reads from a firebird database. The Function works
I have following function which is called continuously from run() of a Thread which
I have the following function which accepts text and a word count and if
I have the following function which sets up the GUI. public void go() {
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
I have the following function which creates a std::vector of iterators into another container:
I have the following function in a PHP script, which returns and API call:
I have the following C++ function definition, which I am trying to call through
I have a for-loop which performs the following function: Take a M by 8
I have added the following fine function to my status bar to show which

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.