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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:22:31+00:00 2026-05-12T19:22:31+00:00

Many thanks for the advice you have given me thus far. Using testbenches is

  • 0

Many thanks for the advice you have given me thus far. Using testbenches is something this forum has really shown me the light on and for that I am appreciative. My problem is that I am playing with a singleton and normally I won’t del it, but in a testbench I will need to. So can anyone show me how to del the thing? I’ve started with a basic example and built it up into a testbench so I can see whats going on. Now I don’t know how to get rid of it!

Many thanks!!

import sys
import logging
import unittest

LOGLEVEL = logging.DEBUG

class Singleton:
    """ A python singleton """

    class __impl:
        """ Implementation of the singleton interface """
        def __init__(self):
            self.log = logging.getLogger()
            self.log.debug("Init %s" % self.__class__.__name__)

        def id(self):
            """ Test method, return singleton id """
            return id(self)


    # storage for the instance reference
    __instance = None

    def __init__(self):
        """ Create singleton instance """
        # Check whether we already have an instance
        if Singleton.__instance is None:
            # Create and remember instance
            Singleton.__instance = Singleton.__impl()

        # Store instance reference as the only member in the handle
        self.__dict__['_Singleton__instance'] = Singleton.__instance

    def __getattr__(self, attr):
        """ Delegate access to implementation """
        return getattr(self.__instance, attr)

    def __setattr__(self, attr, value):
        """ Delegate access to implementation """
        return setattr(self.__instance, attr, value)

class A:
    def __init__(self):
        self.log = logging.getLogger()
        self.log.debug("Init %s" % self.__class__.__name__)
        self.lowclass = Singleton()
        self.id = self.lowclass.id()
        self.log.debug("ID: %s" % self.id)

class B:
    def __init__(self):
        self.log = logging.getLogger()
        self.log.debug("Init %s" % self.__class__.__name__)
        self.lowclass = Singleton()
        self.id = self.lowclass.id()
        self.log.debug("ID: %s" % self.id)


class ATests(unittest.TestCase):

    def testOne(self):
        a = A()
        aid = a.id
        b = B()
        bid = b.id
        self.assertEqual(a.id, b.id)

        #
        # How do I destroy this thing??
        #

        del a
        del b

        a1 = A()
        a1id = a1.id
        self.assertNotEqual(a1id, aid)

if __name__ == '__main__':
    # Set's up a basic logger
    logging.basicConfig( format="%(asctime)s %(levelname)-8s %(module)s %(funcName)s %(message)s", 
                         datefmt="%H:%M:%S", stream=sys.stderr )
    log = logging.getLogger("")
    log.setLevel(LOGLEVEL)
    # 
    suite = unittest.TestLoader().loadTestsFromTestCase(ATests)
    sys.exit(unittest.TextTestRunner(verbosity=LOGLEVEL).run(suite))
  • 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-12T19:22:31+00:00Added an answer on May 12, 2026 at 7:22 pm

    As Borg’s author I obviously second @mjv’s comment, but, with either Borg (aka “monostate”) or Highlander (aka “singleton”), you need to add a “drop everything” method to support the tearDown in your test suite. Naming such method with a single leading underscore tells other parts of the sw to leave it alone, but tests are atypical beasts and often need to muck with such otherwise-internal attributes.

    So, for your specific case,

    class Singleton:
       ...
       def _drop(self):
       "Drop the instance (for testing purposes)."
       Singleton.__instance = None
       del self._Singleton__instance
    

    Similarly, for Borg, a _drop method would release and clear the shared dictionary and replace it with a brand new one.

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

Sidebar

Related Questions

First of all many thanks for clicking into my question. I have a few
Recently thanks to rails' popularity, many people start using activerecord as model. however, before
edit: many thanks for all the answers. Here are the results after applying the
Can anyone recommend any good resources for learning C++ Templates? Many thanks.
I've been a fan of EasyMock for many years now, and thanks to SO
Many times I have seen Visual Studio solutions which have multiple projects that share
Many applications have grids that display data from a database table one page at
In mysql, I have a tree that is represented using the adjacency list model.
Looking for some practical advice here and any experiences people have had in a
I have a project that I am trying to convert to OSGi. However, this

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.