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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:39:11+00:00 2026-05-19T14:39:11+00:00

I have 2 files that print a singleton instance, but I get two different

  • 0

I have 2 files that print a singleton instance, but I get two different instances.

I’m using Singleton code from Gary Robinson.

Here are the files:

test.py

from singleton import Singleton
import untitled

class A(Singleton):
    def __init__(self):
        super(A, self).__init__()        

if __name__ == "__main__":
    a = A.getInstance()
    print a
    untitled.print_a()

untitled.py

def print_a():
    import test
    print test.A.getInstance()

…and here is the output of python test.py

<__main__.A object at 0xaea270>
<test.A object at 0xaea3f0>

Can someone please explain to me what is happening (apparently at the module level) that is causing this behavior?

  • 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-19T14:39:12+00:00Added an answer on May 19, 2026 at 2:39 pm

    The reason you get two singletons is due to the way the modules are being imported. When you execute test.py from the command line, it is not known as test, it is __main__. An entry is added to sys.modules under the name __main__, and execution proceeds. When untitled imports test, sys.modules is examined, no module named test is found, so test.py is executed again to import it. As a result, the class definition for A is executed twice, producing two distinct classes. The Singleton implementation therefore considers them distinct, and produces two instances.

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

Sidebar

Related Questions

I have files in two different directories that I would like to merge the
I have some files that I would like to rename using regex and powershell,
I have some files that are uuencoded, and I need to decode them, using
I have CSV files that have multiple columns that are sorted. For instance, I
I have this code that I have written in Ruby, but when trying to
I have two files that I am trying to work with. menu.py has the
I have wrote a code that can read an excel 2007 file using Microsoft
I have a bunch of CSS files that get loaded on every page (header,
I have Oracle files that I need to compare to CVS files, but the
I have this function that prints the name of all the files in a

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.