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 several files that are in several different languages. I thought they were
I have two PHP files that I need to link. How can I link
I have two properties files that are not the same and I need to
Is there any way to list all the files that have changed between two
I have two files. main.py that has main program logic and functions.py that has
I have an application that prints by generating text files with embedded printer codes,
I have some source files that have comments written in Japanese. When I open
I have 2 wav files that I want to concatenate into one file with
I have potentially large files that need to be sorted by 1-n keys. Some
I have a bunch of perfmon files that have captured information over a period

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.