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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:09:10+00:00 2026-06-11T00:09:10+00:00

I have a singleton like this class Singleton: class __impl: def __init__(self): print INIT

  • 0

I have a singleton like this

class Singleton:

    class __impl:
        def __init__(self):
            print "INIT"

    __instance = None

    def __init__(self):
        # Check whether we already have an instance
        if Singleton.__instance is None:
            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)

When I made several instances of Singleton I get two calls to init , I mean “INIT” is printed two times, and I supose that it shouldn’t happened

Somebody has an idea of what is wrong with this or has a better way to implement this??

  • 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-11T00:09:12+00:00Added an answer on June 11, 2026 at 12:09 am

    Here’s a slightly simpler way to write a Singleton:

    class Singleton(object):
        __instance = None
        def __new__(cls):
            if cls.__instance is None:
                cls.__instance = super(Singleton,cls).__new__(cls)
                cls.__instance.__initialized = False
            return cls.__instance
    
        def __init__(self):      
            if(self.__initialized): return
            self.__initialized = True
            print ("INIT")
    
    a = Singleton()
    b = Singleton()
    print (a is b)
    

    although there may be better ways. I have to admit that I’ve never been fond of singletons. I much prefer a factory type approach:

    class Foo(object):
        pass
    
    def foo_singleton_factory(_singleton= Foo()):
        return _singleton
    
    a = foo_singleton_factory()
    b = foo_singleton_factory()
    print (a is b)
    

    This has the advantage that you can keep getting the same instance of Foo if you want it, but you’re not limited to a single instance if you decide 10 years down the road that you don’t want a true singleton.

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

Sidebar

Related Questions

I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
Let's say I have a singleton class like this: class Settings include Singleton def
If you have a Java Singleton that looks like this: public class MySingleton {
Simple question, I have code like this: class Context[A] { def t: A }
I have a singleton class which looks a lot like this, public class CfgHandler
I have a singleton class for global access to config information. This singleton class
I have 4 classes, one Database helper and 3 defined like this: abstract class
I have a generic class like this: class MyGenericClass<T> { } On the other
I have a couple classes that look like this public class ParentClass { public
I have a singleton class with this code: manager.h @interface Manager : NSObject {

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.