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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:41:13+00:00 2026-06-13T21:41:13+00:00

I have a sample doctest like this one. This is the iniFileGenerator module. >>>

  • 0

I have a sample doctest like this one.

"""
This is the "iniFileGenerator" module.
>>> hintFile = "./tests/unit_test_files/hint.txt"
>>> f = iniFileGenerator(hintFile)
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
    def __init__(self, hintFilePath):
        self.hintFilePath = hintFilePath
    def hello(self):
        """
        >>> f.hello()
        hello
        """
        print "hello"
if __name__ == "__main__":
    import doctest
    doctest.testmod()

When I execute this code, I got this error.

Failed example:
    f.hello()
Exception raised:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.iniFileGenerator.hello[0]>", line 1, in <module>
        f.hello()
    NameError: name 'f' is not defined

This error is caused by accessing ‘f’ which was not accessible when testing hello() method.

Is there any way to share the object which is created before? Without it, one need to create object all the time when it’s necessary.

def hello(self):
    """
    hintFile = "./tests/unit_test_files/hint.txt"
    >>> f = iniFileGenerator(hintFile)
    >>> f.hello()
    hello
    """
    print "hello"
  • 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-13T21:41:15+00:00Added an answer on June 13, 2026 at 9:41 pm

    You can use testmod(extraglobs={'f': initFileGenerator('')}) to define a reusable object globally.

    As the doctest doc says,

    extraglobs gives a dict merged into the globals used to execute examples. This works like dict.update()

    But I used to test all methods in __doc__ of class before all methods.

    class MyClass(object):
        """MyClass
        >>> m = MyClass()
        >>> m.hello()
        hello
        >>> m.world()
        world
        """
    
        def hello(self):
            """method hello"""
            print 'hello'
    
        def world(self):
            """method world"""
            print 'world'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sample text like this: Nytimes.com One of the biggest news portals, have
I have sample XML like this: <users> <user name=user1> <roles separator=,>ADM,USER</roles> </user> <user name=user2>
I have sample code like this: <div class=cart> <a onclick=addToCart('@Model.productId'); class=button><span>Add to Cart</span></a> </div>
I have sample data like this df <- data.frame(name = rep(letters[1:7], each = 24),
I have sample vector like this: v <- c(1, 2, 1, 3, 2, 3,
I have developed sample api as jar file. This jar file contains the code
I have a sample class where I need to specialize the Print function if
I have this sample page: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8> <title>Ajax Page</title> <script
Does any one have sample code to apply phone number formatting to certain fields???
I have sample java code like below. String testEfdDirectoryPath=D:\\test; String efdExecutable = test.cmd; File

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.