Creating a singleton in Python

534,227

Solution 1

Use a Metaclass

I would recommend Method #2, but you're better off using a metaclass than a base class. Here is a sample implementation:

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]
        
class Logger(object):
    __metaclass__ = Singleton

Or in Python3

class Logger(metaclass=Singleton):
    pass

If you want to run __init__ every time the class is called, add

        else:
            cls._instances[cls].__init__(*args, **kwargs)

to the if statement in Singleton.__call__.

A few words about metaclasses. A metaclass is the class of a class; that is, a class is an instance of its metaclass. You find the metaclass of an object in Python with type(obj). Normal new-style classes are of type type. Logger in the code above will be of type class 'your_module.Singleton', just as the (only) instance of Logger will be of type class 'your_module.Logger'. When you call logger with Logger(), Python first asks the metaclass of Logger, Singleton, what to do, allowing instance creation to be pre-empted. This process is the same as Python asking a class what to do by calling __getattr__ when you reference one of it's attributes by doing myclass.attribute.

A metaclass essentially decides what the definition of a class means and how to implement that definition. See for example http://code.activestate.com/recipes/498149/, which essentially recreates C-style structs in Python using metaclasses. The thread What are some (concrete) use-cases for metaclasses? also provides some examples, they generally seem to be related to declarative programming, especially as used in ORMs.

In this situation, if you use your Method #2, and a subclass defines a __new__ method, it will be executed every time you call SubClassOfSingleton() -- because it is responsible for calling the method that returns the stored instance. With a metaclass, it will only be called once, when the only instance is created. You want to customize what it means to call the class, which is decided by it's type.

In general, it makes sense to use a metaclass to implement a singleton. A singleton is special because is created only once, and a metaclass is the way you customize the creation of a class. Using a metaclass gives you more control in case you need to customize the singleton class definitions in other ways.

Your singletons won't need multiple inheritance (because the metaclass is not a base class), but for subclasses of the created class that use multiple inheritance, you need to make sure the singleton class is the first / leftmost one with a metaclass that redefines __call__ This is very unlikely to be an issue. The instance dict is not in the instance's namespace so it won't accidentally overwrite it.

You will also hear that the singleton pattern violates the "Single Responsibility Principle" -- each class should do only one thing. That way you don't have to worry about messing up one thing the code does if you need to change another, because they are separate and encapsulated. The metaclass implementation passes this test. The metaclass is responsible for enforcing the pattern and the created class and subclasses need not be aware that they are singletons. Method #1 fails this test, as you noted with "MyClass itself is a a function, not a class, so you cannot call class methods from it."

Python 2 and 3 Compatible Version

Writing something that works in both Python2 and 3 requires using a slightly more complicated scheme. Since metaclasses are usually subclasses of type type, it's possible to use one to dynamically create an intermediary base class at run time with it as its metaclass and then use that as the baseclass of the public Singleton base class. It's harder to explain than to do, as illustrated next:

# works in Python 2 & 3
class _Singleton(type):
    """ A metaclass that creates a Singleton base class when called. """
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Singleton(_Singleton('SingletonMeta', (object,), {})): pass

class Logger(Singleton):
    pass

An ironic aspect of this approach is that it's using subclassing to implement a metaclass. One possible advantage is that, unlike with a pure metaclass, isinstance(inst, Singleton) will return True.

Corrections

On another topic, you've probably already noticed this, but the base class implementation in your original post is wrong. _instances needs to be referenced on the class, you need to use super() or you're recursing, and __new__ is actually a static method that you have to pass the class to, not a class method, as the actual class hasn't been created yet when it is called. All of these things will be true for a metaclass implementation as well.

class Singleton(object):
  _instances = {}
  def __new__(class_, *args, **kwargs):
    if class_ not in class_._instances:
        class_._instances[class_] = super(Singleton, class_).__new__(class_, *args, **kwargs)
    return class_._instances[class_]

class MyClass(Singleton):
  pass

c = MyClass()

Decorator Returning A Class

I originally was writing a comment but it was too long, so I'll add this here. Method #4 is better than the other decorator version, but it's more code than needed for a singleton, and it's not as clear what it does.

The main problems stem from the class being it's own base class. First, isn't it weird to have a class be a subclass of a nearly identical class with the same name that exists only in its __class__ attribute? This also means that you can't define any methods that call the method of the same name on their base class with super() because they will recurse. This means your class can't customize __new__, and can't derive from any classes that need __init__ called on them.

When to use the singleton pattern

Your use case is one of the better examples of wanting to use a singleton. You say in one of the comments "To me logging has always seemed a natural candidate for Singletons." You're absolutely right.

When people say singletons are bad, the most common reason is they are implicit shared state. While with global variables and top-level module imports are explicit shared state, other objects that are passed around are generally instantiated. This is a good point, with two exceptions.

The first, and one that gets mentioned in various places, is when the singletons are constant. Use of global constants, especially enums, is widely accepted, and considered sane because no matter what, none of the users can mess them up for any other user. This is equally true for a constant singleton.

The second exception, which get mentioned less, is the opposite -- when the singleton is only a data sink, not a data source (directly or indirectly). This is why loggers feel like a "natural" use for singletons. As the various users are not changing the loggers in ways other users will care about, there is not really shared state. This negates the primary argument against the singleton pattern, and makes them a reasonable choice because of their ease of use for the task.

Here is a quote from http://googletesting.blogspot.com/2008/08/root-cause-of-singletons.html:

Now, there is one kind of Singleton which is OK. That is a singleton where all of the reachable objects are immutable. If all objects are immutable than Singleton has no global state, as everything is constant. But it is so easy to turn this kind of singleton into mutable one, it is very slippery slope. Therefore, I am against these Singletons too, not because they are bad, but because it is very easy for them to go bad. (As a side note Java enumeration are just these kind of singletons. As long as you don't put state into your enumeration you are OK, so please don't.)

The other kind of Singletons, which are semi-acceptable are those which don't effect the execution of your code, They have no "side effects". Logging is perfect example. It is loaded with Singletons and global state. It is acceptable (as in it will not hurt you) because your application does not behave any different whether or not a given logger is enabled. The information here flows one way: From your application into the logger. Even thought loggers are global state since no information flows from loggers into your application, loggers are acceptable. You should still inject your logger if you want your test to assert that something is getting logged, but in general Loggers are not harmful despite being full of state.

Solution 2

class Foo(object):
     pass

some_global_variable = Foo()

Modules are imported only once, everything else is overthinking. Don't use singletons and try not to use globals.

Solution 3

Use a module. It is imported only once. Define some global variables in it - they will be singleton's 'attributes'. Add some functions - the singleton's 'methods'.

Solution 4

You probably never need a singleton in Python. Just define all your data and functions in a module and you have a de facto singleton:

import datetime
file_name=None

def set_file_name(new_file_name: str):
    global file_name
    file_name=new_file_name

def write(message: str):
    global file_name
    if file_name:
        with open(file_name, 'a+') as f:
            f.write("{} {}\n".format(datetime.datetime.now(), message))
    else:
        print("LOG: {}", message)

To use:

import log
log.set_file_name("debug.log")
log.write("System starting")
...

If you really absolutely have to have a singleton class then I'd go with:

class MySingleton(object):
    def foo(self):
        pass

my_singleton = MySingleton()

To use:

from mysingleton import my_singleton
my_singleton.foo()

where mysingleton.py is your filename that MySingleton is defined in. This works because after the first time a file is imported, Python doesn't re-execute the code.

Solution 5

Here's a one-liner for you:

singleton = lambda c: c()

Here's how you use it:

@singleton
class wat(object):
    def __init__(self): self.x = 1
    def get_x(self): return self.x

assert wat.get_x() == 1

Your object gets instantiated eagerly. This may or may not be what you want.

Share:
534,227
theheadofabroom
Author by

theheadofabroom

Updated on May 14, 2022

Comments

  • theheadofabroom
    theheadofabroom about 2 years

    This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In this instance I define 'most pythonic' to mean that it follows the 'principle of least astonishment'.

    I have multiple classes which would become singletons (my use-case is for a logger, but this is not important). I do not wish to clutter several classes with added gumph when I can simply inherit or decorate.

    Best methods:


    Method 1: A decorator

    def singleton(class_):
        instances = {}
        def getinstance(*args, **kwargs):
            if class_ not in instances:
                instances[class_] = class_(*args, **kwargs)
            return instances[class_]
        return getinstance
    
    @singleton
    class MyClass(BaseClass):
        pass
    

    Pros

    • Decorators are additive in a way that is often more intuitive than multiple inheritance.

    Cons

    • While objects created using MyClass() would be true singleton objects, MyClass itself is a function, not a class, so you cannot call class methods from it. Also for

      x = MyClass();
      y = MyClass();
      t = type(n)();
      

    then x == y but x != t && y != t


    Method 2: A base class

    class Singleton(object):
        _instance = None
        def __new__(class_, *args, **kwargs):
            if not isinstance(class_._instance, class_):
                class_._instance = object.__new__(class_, *args, **kwargs)
            return class_._instance
    
    class MyClass(Singleton, BaseClass):
        pass
    

    Pros

    • It's a true class

    Cons

    • Multiple inheritance - eugh! __new__ could be overwritten during inheritance from a second base class? One has to think more than is necessary.

    Method 3: A metaclass

    class Singleton(type):
        _instances = {}
        def __call__(cls, *args, **kwargs):
            if cls not in cls._instances:
                cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
            return cls._instances[cls]
    
    #Python2
    class MyClass(BaseClass):
        __metaclass__ = Singleton
    
    #Python3
    class MyClass(BaseClass, metaclass=Singleton):
        pass
    

    Pros

    • It's a true class
    • Auto-magically covers inheritance
    • Uses __metaclass__ for its proper purpose (and made me aware of it)

    Cons

    • Are there any?

    Method 4: decorator returning a class with the same name

    def singleton(class_):
        class class_w(class_):
            _instance = None
            def __new__(class_, *args, **kwargs):
                if class_w._instance is None:
                    class_w._instance = super(class_w,
                                        class_).__new__(class_,
                                                        *args,
                                                        **kwargs)
                    class_w._instance._sealed = False
                return class_w._instance
            def __init__(self, *args, **kwargs):
                if self._sealed:
                    return
                super(class_w, self).__init__(*args, **kwargs)
                self._sealed = True
        class_w.__name__ = class_.__name__
        return class_w
    
    @singleton
    class MyClass(BaseClass):
        pass
    

    Pros

    • It's a true class
    • Auto-magically covers inheritance

    Cons

    • Is there not an overhead for creating each new class? Here we are creating two classes for each class we wish to make a singleton. While this is fine in my case, I worry that this might not scale. Of course there is a matter of debate as to whether it aught to be too easy to scale this pattern...
    • What is the point of the _sealed attribute
    • Can't call methods of the same name on base classes using super() because they will recurse. This means you can't customize __new__ and can't subclass a class that needs you to call up to __init__.

    Method 5: a module

    a module file singleton.py

    Pros

    • Simple is better than complex

    Cons

    • Not lazily instantiated
    • Chris Morgan
      Chris Morgan almost 13 years
      Another three techniques: use a module instead (often - generally, I think - this is a more appropriate pattern for Python but it depends a bit on what you're doing with it); make a single instance and deal with it instead (foo.x or if you insist Foo.x instead of Foo().x); use class attributes and static/class methods (Foo.x).
    • Cat Plus Plus
      Cat Plus Plus almost 13 years
      @ChrisMorgan: If you're going to use class/static methods only, then don't bother making a class, really.
    • agf
      agf almost 13 years
    • Chris Morgan
      Chris Morgan almost 13 years
      @Cat: yep. The third case would tend to be better expressed as the first or second.
    • theheadofabroom
      theheadofabroom almost 13 years
      @Cat: The effect is similar, however the reasons behind creating a global variable can be just about anything, including not knowing any better. Why does one create a singleton? If you have to ask you shouldn't be here. This explicitness is not only more pythonic, but makes maintenance a lot more simple. Yes singletons are syntactic sugar for globals, but then classes are syntactic sugar for a whole bunch of unsightly stuff and I don't think anyone will tell you you're always better off without them.
    • theheadofabroom
      theheadofabroom almost 13 years
      Maybe it would help if I were to explain the purpose. I wish to have a few different logging classes that work in different ways with a variety of different sources/destinations. Now I want to apply these loggers to a variety of different functions as a decorator, and only need one logger of each type, init'd against many different functions. I.e. One that appends to a text file, one prints stdout, one prints stderr, and another sets a trace for pdb. These would all then be triggered differently at different logging levels. To me logging has always seemed a natural candidate for Singletons.No?
    • theheadofabroom
      theheadofabroom almost 13 years
      @Cat Anyway, whether or not Singletons are a good idea was never supposed to be the question. The question was supposed to be how they are best implemented. I'd guess my question will not be reopened, which is a shame. Anyhow, I wouldn't have described you as rude - I think a lively debate is always interesting, it's just rather not the point of stackoverflow.
    • agf
      agf almost 13 years
      @BiggAl You copied the wrong code from my post. That's my fixes to Method #2, not my metaclass. I saw you want to run __init__ every time; I added that code to my post. I also commented on Method #4. Summary: it's bad for a class to be its own base class.
    • theheadofabroom
      theheadofabroom almost 13 years
      @agf Cheers for the edit, as I couldn't see the problem, as the method I had put up was a tweak of yours. To be honest, other than s/object/type/g, I don't see the problem - in yours a dict is shared with all Singleton type classes yes? And the __new__ method selects the correct one by using the class as its hash, creating it if not already done. In my version each class had access to only its own instance, or the base instance if that had been created, and then the __new__ method checks that it is an instance of the correct class. This surely shares less state ergo so is more desirable?
    • agf
      agf almost 13 years
      @BiggAl The other problem is __call__ vs. __new__. In a metaclass, __new__ means when the class is new, not when the instance is new. You want the method called every time someone tries to make a new instance so you need to use __call__. Even if you fix your version to use type and super instead of object, it won't make singletons unless you use __call__ -- test it. _instance vs. _instances wasn't the problem. Your way might even be better, I have to think about it.
    • theheadofabroom
      theheadofabroom almost 13 years
      Ah cool, I hadn't noticed that. Metaclasses seem very powerful, do you know of any resources that would show a little more about them? I found this question, but do you know of any other great resources?
    • agf
      agf almost 13 years
      Check out the other SO questions that come up for "Metaclass Python". Read all of docs.python.org/reference/datamodel.html. Recently I saw bitshaq.com/2011/07/14/basic-intro-to-python-meta-programmin‌​g which looks pretty good. Read secure.wikimedia.org/wikipedia/en/wiki/Metaclass. Just keep thinking "it's a class who's instances are class definitions".
    • Hejazzman
      Hejazzman over 8 years
      The anti-signletons sentiment is cargo cult programming at its worst. Same with people hearing (few bothered to actually read) "Goto statement considered harmful" and think gotos are a sign of bad code regardless of context.
    • deepak
      deepak about 8 years
      Why can't i say that a class with only class methods and class attributes is a singleton class ?
    • theheadofabroom
      theheadofabroom about 8 years
      @deepak because strictly speaking that's a static class, or as close as you'd get in python. You can still add instance attributes unless you prevent instantiation, at which point it becomes more truly static
    • deepak
      deepak about 8 years
      @theheadofabroom but in python you can always use setattr(object,name,value) to add instance variable, i don't see any concrete reason of why should use any of those methods explained below to implement singleton, when python itself by default supports it. Let me know if i am missing anything.
    • theheadofabroom
      theheadofabroom about 8 years
      With a Singleton you have only a single instance, ergo you can setattr without any inconsistencies. This is why I was saying that for a static class you would have to prevent instantiation
    • Pankaj Singhal
      Pankaj Singhal almost 8 years
      @agf I need to make singleton on the basis of arguments instead of class names. I've posted a question here on SO -> stackoverflow.com/questions/39033946/…
    • chrisvp
      chrisvp almost 8 years
      Hi, thanks for your elaborate post. I am fairly new to pattern programming and to python actually, and I am surprised that although method 2 seems to most well known one (it s everywhere), hardly ever someone mentions that despite only one object is created, init__() is called every time Singleton() or MyClass() are used anywhere. I didn't try, but AFAIK this is true for all other methods too. This hardly seems desirable when implementing a singleton, or am i missing something? Of course the solution consists of setting an attribute to avoid performing __init twice. Just curious
    • theheadofabroom
      theheadofabroom almost 8 years
      @chrisvp indeed you probably want some form of guard around init, although I'd also say that you very rarely want a Singleton, over the years since this question was written, I've only really needed a Singleton twice, and one of those was taken out when I made things more functional, and therefore this shared state became undesirable
    • Lior Magen
      Lior Magen over 7 years
      How would you kill the object of the 3rd method?
    • theheadofabroom
      theheadofabroom over 7 years
      @LiorMagen You wouldn't - how would you know that it wasn't going to instantiated again?
    • Lior Magen
      Lior Magen over 7 years
      @theheadofabroom I wouldn't, this will be relevant if I want to delete an existing object and create a new one instead of it. No way of doing that?
    • Gaurav Parashar
      Gaurav Parashar over 6 years
      How about implementing it as a module?
    • tammoj
      tammoj about 5 years
      Method 3 solved my problem with threading: class MyClass(threading.Thread, metaclass=Singleton):
    • JeffCharter
      JeffCharter over 4 years
      @hejazzman further, the metaclass method negates the reason singletons are considered bad. For testing, you can take away the singleton-ness of the object so you no longer have shared state.
    • theheadofabroom
      theheadofabroom over 4 years
      @JeffCharter I'm not sure this is the only reason singletons are sometimes considered an antipattern, but you're right that it does allow you to negate that particular issue. In a lot of cases the issue is not the fact that you use a singleton, but that ending up with an architecture where a singleton is useful often points to other architectural issues. Obviously this is a rule of thumb rather than anything hard and fast, which is why I'd still support the usefulness of my question all these years later ;)
    • dvlper
      dvlper over 3 years
      Saw (following). Yet, another way of implementing singleton pattern - apart from the answers to this post. Hope it helps! tutorialspoint.com/python_design_patterns/…
    • Jeyekomon
      Jeyekomon about 3 years
      Are there any drawbacks of implementing the singleton class as a regular class and then just telling the users that it is not much useful to create multiple instances of it? As in the spirit of "we are all consenting adults here".
    • theheadofabroom
      theheadofabroom about 3 years
      @Jeyekomon only in so far as without enforcement it's easy to make mistakes, which can then often be difficult to debug
    • 00001H
      00001H over 2 years
      HEY!!!!!MAJOR CONS FOR METACLASS IMPLEMENTATION!!! If you do class MoreSingleton(Singleton,metaclass=OtherMeta) it will say TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
    • eri0o
      eri0o over 2 years
      how does Method 3 usage code actually looks like? How do I access a method from it from elsewhere? I get Parameter 'self' unfilled
  • Cat Plus Plus
    Cat Plus Plus almost 13 years
    No, singletons are never good. Logging might be a good candidate for being a global (as terrible as they are), but certainly not singleton.
  • agf
    agf almost 13 years
    Look at googletesting.blogspot.com/2008/08/…. It is generally anti-singleton (for good reason) but it has a good explaination of why immutable singletons and singletons without side effects don't have the same problems, if you're careful. I'm going to quote it a bit at the end of my post.
  • Cat Plus Plus
    Cat Plus Plus almost 13 years
    My problem with singletons is the stupid premise of "only one instance". That and tonne of thread safety problems. And dependency hiding. Globals are bad, and singletons are just globals with more problems.
  • theheadofabroom
    theheadofabroom almost 13 years
    @agf I'll probably accept and award this answer, but shall give a little time for others. I've not worked with metaclasses before, and although I've had a quick google, the answer may be more valuable to future readers if some more explanation could be given on how these are used, what they effect, and any pitfalls to watch out for.
  • theheadofabroom
    theheadofabroom almost 13 years
    What is the point of the _sealed attribute? As far as I see this doesn't do anything? Something is niggling me about this that says it shouldn't perform well... I'll run some comparative tests later this week.
  • theheadofabroom
    theheadofabroom almost 13 years
    So what you end up with is... Not a class. You can't use it as a class, you can't base other classes upon it, you use import syntax, and all of a sudden you lose all the benefits of OOP...
  • theheadofabroom
    theheadofabroom almost 13 years
    While this is not really an answer to my question, the resources you point to are very useful. I begrudgingly give you a +1
  • Guard
    Guard almost 13 years
    _sealed ensures your init is ran only once; I don't see any point why it should perform worse than the regular function-like decorator - the function is executed only once per class, and returns a new inherited class
  • Guard
    Guard almost 13 years
    BTW, your edit contains tabs that break the indents You also say 'we are creating 2 classes' - do you mean that we are creating '1 extra class'?
  • SingleNegationElimination
    SingleNegationElimination almost 13 years
    if you can base other classes on it, then it might not be a singleton. you could create one of the derived class, but also one of the base class, but the derived class is also a member of the base, and you have two of the base, which one are you supposed to use?
  • theheadofabroom
    theheadofabroom almost 13 years
    Yes one extra class is what I meant. I intend to include stuff in __init__ to be called every time it's initialised. Just a simple 'Being initialised in class.method'. as for indentation - you used tabs and spaces - I fixed most of it, but seem to have missed one if you want to get it (just check the edit log)
  • Guard
    Guard almost 13 years
    re init: of course it's up to you, I just tried to mimic the singleton behavior in other languages, where constructor code (which is not exactly init, but very close in its sense) is only called once if you want the init to be called every time, just kill all references to _sealed re spaces / tabs - well, then my emacs needs fixing. anyway, above is the corrected version
  • agf
    agf almost 13 years
    @Guard I commented on your method in my post. Basically, the class shouldn't need to know / care it's a singleton. It should be able to customize __new__ and safely call super() -- which your method doesn't allow, as calling super() on a method of the same name will recurse.
  • Guard
    Guard almost 13 years
    @agf, I think you are a bit exaggerating about names related to the inheritance chain: gist.github.com/1108823 We are just altering it's string name, not a class variable
  • agf
    agf almost 13 years
    It's not the name of the class that matters. Try your own code: have MyClass.__init__(text) call super(MyClass, self).__init__(text) or add MyClass.__new__(cls) method and have it call return super(self, MyClass).__new__(cls). You'll recurse infinitely.
  • agf
    agf almost 13 years
    @BiggAl - You've mis-copied my code into your post -- that's my corrections to #2 not my metaclass. Also, you missed an important problem with that particular implementation of a decorator returning a class.
  • Paul Manta
    Paul Manta almost 13 years
    @Cat There are very good uses for singletons. Lazy instantiation of hardware modules (especially in single threaded applications) is one of them (but thread safe singletons also exist).
  • Paul Manta
    Paul Manta almost 13 years
    @Cat Another use came to mind (this is an example from the game I'm writing atm). I have an EventManager class written in C++ and exposed to Python. I need both languages to queue events in the same manager. I could, somehow, pass the same instance of the manager as a parameter to functions in both languages, but that would require a lot of work and the benefits would literally be nil. Actually, it might make the code harder to follow...
  • Alcott
    Alcott almost 13 years
    why did you say "Don't use singletons"? Any reason?
  • Alcott
    Alcott almost 13 years
    @agf, when implementing Singleton through metaclass, why did you use "call" not "new"? and What's the difference?
  • agf
    agf almost 13 years
    @Alcott __new__ in a metaclass is when the class is new -- when it's defined, not when the instance would be new. Calling the class (MyClass()) is the operation you want to override, not the definition of the class. If you really want to understand how Python works, the best thing you can do (other than keep using it) is read docs.python.org/reference/datamodel.html. A good reference on metaclasses is eli.thegreenplace.net/2011/08/14/python-metaclasses-by-examp‌​le. A good article on singletons is the series from the google blog I linked in this answer.
  • agf
    agf almost 13 years
    Also, see stackoverflow.com/questions/31875/… for more singleton stuff if you haven't found that yet.
  • dividebyzero
    dividebyzero over 12 years
    This won't work if the singleton has to be pickled. Using the example you gave: s = some_global_variable; str = pickle.dumps(s); s1 = pickle.loads(str); print s is s1; # False
  • ovgolovin
    ovgolovin almost 12 years
    @agf Could you explain why super(MyClass, self).__init__(text) recurses? As I understand, super(MyClass, self) should be object.
  • ovgolovin
    ovgolovin almost 12 years
    @agf I made a special question for that: stackoverflow.com/questions/11811935/…
  • agf
    agf over 11 years
    @martineau It's an easy mistake to make. Python 3 has improved the metaclass syntax by making it a keyword argument: Logger(metaclass = Singleton).
  • theheadofabroom
    theheadofabroom almost 11 years
    surely this is not a metaclass - in python3 to use a metaclass to construct MyClass you would do class MyClass(metaclass=Singleton)
  • Jonas Kölker
    Jonas Kölker over 10 years
    @dividebyzero: the is operator tests for pointer equality. I would be rather surprised---to the point of calling it a bug---if pickle.loads returned a reference to a pre-existing object rather than a reference to a newly created one. Thus, testing whether s is s1 doesn't tell you anything about the suitability of using modules as singletons.
  • Dan Passaro
    Dan Passaro about 10 years
    @JonasKölker pickle.loads() does do that already, e.g. for instances of bool and NoneType. pickle.loads(pickle.dumps(False)) is False yields True
  • Jonas Kölker
    Jonas Kölker about 10 years
    @leo-the-manic: fair point; however, that's just a side effect of Python interning the objects True, False and None, and has nothing to do with the code behind pickle.loads. Also, it's safe to do only for read-only objects. If pickle.loads were to return a reference to an already existing modifiable object—such as a module—that would be a bug. (And so I'm standing by my implication that dividebyzero's code example doesn't prove anything.)
  • FichteFoll
    FichteFoll over 9 years
    Instead of class Singleton(_Singleton('SingletonMeta', (object,), {})): pass you could remove one class by using an assignment: Singleton = _Singleton('Singleton', (object,), {}).
  • 0xc0de
    0xc0de over 9 years
    You don't define a class to use it as an object.
  • Tolli
    Tolli over 9 years
    Why do you need to know use the class of a singleton? Just use the singleton object..
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    why this is a terrible approach? when you want create another singleton class, you have to copy & paste the __new__. Don't repeat yourself.
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    It's not singleton pattern, so IMO the function should be named differently.
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    It's not true singleton. SingletonList = Singleton(list).Instance(); print(SingletonList is type(SingletonList)()) should print True in true singleton; with your code prints False
  • Paul Manta
    Paul Manta over 9 years
    @GingerPlusPlus I was aware of a few limitations, but not of the one you pointed out. Thanks for mentioning it. Unfortunately, I don't have time at the moment to thin about a solution to this.
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    Every time you call type(MySingleton)(), MySingleton.__init__() gets called and the object gets initialized multiple Times; you can fix it writing cls.__init__ = lambda self: pass in your singleton. Also, overriding cls.__call__ seems pointless, and even harmful - __call__ defined in this context is used when you call MySingleton(any, list, of, arguments), not when you call type(MySingleton)(any, list, of, arguments).
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    Also, why your new takes *args and **kwargs, and then does nothing with them? Pass them into dict.__new__ this way: dict.__new__(cls, *args, **kwargs).
  • Tolli
    Tolli over 9 years
    @GingerPlusPlus, Thanks for pointing out that __init__() gets called again when doing type(MySingleton)(). The solution you proposed (adding cls.__init__ = lambda self: pass) give a syntax error, because the last part of the lambda expression needs to be an expression, not a statement. However, adding cls.__init__ = lambda self: None works, so I added that to my answer.
  • Tolli
    Tolli over 9 years
    @GingerPlusPlus, Regarding the use of __call__. my intention was to make both type(MySingleton)() and MySingleton() return the instance. So it is doing what I wanted. You can think of MySingleton as either the type of the singleton or the instance of the singleton (or both).
  • Jonas Kölker
    Jonas Kölker over 9 years
    Wikipedia: "the singleton pattern is a design pattern that restricts the instantiation of a class to one object". I would say that my solution does just that. Okay, I guess one could do wat2 = type(wat)(), but this is python, we're all consenting adults and all that. You can't guarantee that there will be only one instance, but you can guarantee that if people make a second one, it will look ugly and—if they're decent, upstanding people—like a warning sign to them. What am I missing?
  • Malina
    Malina over 9 years
    @agf: May I ask you to elaborate on what you think is the right way to instantiate the internal state of the Logger class when using the metaclass Singleton? In particular, I find myself waffling on whether it is better to implement Logger.__init__ in order to lazily instantiate Logger's state, or to initialize Logger's state as a class member (not lazy, but more clear).
  • Eastsun
    Eastsun about 9 years
    Is it better to use Singletone._instances rather than cls._instances in the __call__ function? Otherwise the classes of Singletone may define its own class variable _instances and conflict with Singletone's.
  • agf
    agf about 9 years
    @Eastsun If you're worried about that, then the right thing to do would be to use __instances. See the name mangling docs for more info. That will prevent it being accidentally overridden without depending on the name of the class.
  • Jonathan
    Jonathan almost 9 years
    @Christophe, consider also inheritance. Perhaps an App singleton, and lots of different types of Apps that inherent from that (LinuxApp, MobileApp, DesktopApp) etc
  • theheadofabroom
    theheadofabroom over 8 years
    While this is a slightly different interpretation of the singleton pattern, I'm pretty sure it's still valid, although I might be tempted to use __new__ rather than __init__, as it purely acts on class attributes and this prevents there from briefly being a second instance. The difference then between this and method 2, is whether trying to initialise more than once returns the single instance or raises an exception. I think I'm happy that either satisfy the singleton pattern, one is easier to use, while the other is more explicit that it is a singleton.
  • theheadofabroom
    theheadofabroom over 8 years
    Obviously use of the class name in __init__ prevents subclassing, but while this makes things easier, it's not required
  • mike rodent
    mike rodent over 8 years
    Thanks... ah yes, a momentary second instance before the Exception gets thrown. I have modified the __init__ so that hopefully this should be subclassable...
  • theheadofabroom
    theheadofabroom over 8 years
    Cool, the could probably benefit from being a class method for similar reasons
  • mike rodent
    mike rodent over 8 years
    yes, you're right. Then you can have a SuperElvis subclass singleton and (for example) an ImaginaryElvis subclass singleton... and they can co-exist. See additional thoughts. Please feel free to improve my code.
  • mike rodent
    mike rodent over 8 years
    Thanks for the code improvements. Just one minor quibble/doubt: making instance a class field like this means that King.instance evaluates to None before the singleton is created, rather than raising a "no attribute" exception... Never mind though. Arguably more desirable: I don't know what reasoning to apply...
  • mike rodent
    mike rodent over 8 years
    oops, also had to tweak your the version to reflect use of class field
  • Peter Mortensen
    Peter Mortensen over 8 years
    The mikewatkins.ca link is (effectively) broken.
  • deepak
    deepak about 8 years
    @Cat Plus Plus I think this will significantly increase the import time of the module. It is always better lazy create the object, when required.
  • Pankaj Singhal
    Pankaj Singhal almost 8 years
    @agf I want to return object on the basis of arguments, not on class name basis. Can someone provide a solution of my question which I've posted here -> stackoverflow.com/questions/39033946/…
  • mikenerone
    mikenerone almost 8 years
    Mostly true, but sometimes that's not enough. E.g. I have a project with a need to log instantiations of many classes at DEBUG level. I need command line options parsed at startup in order to set the user-specified logging level before those classes are instantiated. Module-level instantiations make that problematic. It's possible that I could carefully structure the app so that all of those classes don't get imported until the CLI processing is done, but natural structure of my app is more important than dogmatic adherence to "singletons are bad", since they can be done quite cleanly.
  • Paul Kenjora
    Paul Kenjora over 7 years
    This doesn't work across modules. In my "main" module i set a value. I then reference it in another module and its null. Sigh.
  • warvariuc
    warvariuc over 7 years
    @PaulKenjora You must have an error in your code. If you define a global variable in a module, when you access it from another module it should have the value.
  • Paul Kenjora
    Paul Kenjora about 7 years
    It has the value but when I change it it is not preserved. I ended up using a class with properties in a module that works. Simple types as globals did not work for me ( they lost values as soon as scope changed ).
  • Chen A.
    Chen A. almost 7 years
    @agf you mentined that _instances needs to be called on the cls level, and said it is not defined in the cls namespace. How is that possible? on which namespace does the cls._instances attribute defined?
  • agf
    agf almost 7 years
    @Vinny I'm not totally sure I understand your question, feel free to clarify. In my example, what I'm saying is you can't accidentally do self._instances and override it in an instance of e.g. Logger. You'd have to do Logger._instances or type(self)._instances` or similar. So if other classes in multiple inheritance also use the name _instances, you're still generally going to be save from accidentally overriding it. Also as I mentioned in a comment, if you double the underscore Python will further hide the variable and make it even safer.
  • agf
    agf almost 7 years
    Also, @Vinny -- was this post linked from a blog post of something? It seems like it's been getting a lot of votes recently; before that I'd totally forgot about it.
  • Chen A.
    Chen A. almost 7 years
    @agf I got it now, after some expirement. My doubt was where the _instances variable is actually saved (namespace), and I understand what you mean in your comment. I got to this by another answer of yours where you linked to this thread for further explanation. It is defiantly worth to be mentioned in a blogpost :-)
  • Jan DB
    Jan DB over 6 years
    This only works if all the imports happen the same way. import project.module and import .module will run the code twice.
  • agf
    agf over 6 years
    @MichaelBerdyshev It should in Python 3.
  • mike rodent
    mike rodent over 6 years
    2017-12-23 downvote... please have the courtesy to add a comment to explain your thinking: it might be helpful to me, or you might find that you learn something
  • polvoazul
    polvoazul over 6 years
    @theheadofabroom you could import * from base_module... rethink OOP my friend! hahahah
  • ThorSummoner
    ThorSummoner over 6 years
    I have learned that modules can actually be imported multiple times, in such case this is only a local singleton, which isn't really a singleton in any capacity.
  • Naveen
    Naveen about 6 years
    if you were to test your code while patching my_singleton, would that be possible ? since this my_singleton could be instantiated in some other module.
  • Naveen
    Naveen about 6 years
    If some_global_variable is used in some_module.py and that was the module under test, would mocking some_global_variable be possible?
  • Alan Dyke
    Alan Dyke about 6 years
    @Naveen - my_singleton is a single object. If you "patch" it that change will affect all future references, even in other modules.
  • user2128702
    user2128702 almost 6 years
    How about using a Singleton class in order to manage hardware components? Imagine, you have LCD display (on your RPi for example) and you wrote a small python script called displayManager.py. I think it completely fits into the "the most common reason is they are implicit shared state" definition mention here. If many other components are using this LCD display, they are using a shared state somehow. Why not doing so via a Singleton class.
  • Aran-Fey
    Aran-Fey over 5 years
    This will call the __init__ method every time the class is called. If your __init__ method actually did something, you'd notice the problem. Whenever you do SomeSingleton(), your singleton's state is reset by the __init__ method.
  • astafan8
    astafan8 over 5 years
    Additional question: should one use weakref.WeakValueDictionary for _instances so that the instances get automagically removed when they are not used anymore? Imagine a "singleton" for DLL wrappers, where instances can be distinguished by path to DLL.
  • sleblanc
    sleblanc over 5 years
    Can you elaborate on how a module can be imported multiple times? The only time I have seen that is when an exception occurs while the module is loaded, the user may still load the module later, yet the side effects will have occurred already, so some actions may be executed a second time.
  • sleblanc
    sleblanc over 5 years
    Once a module has been fully loaded, I don't see a way to have this module run again, other than by coercing the interpreter into doing it using eval or importlib.reload.
  • nn0p
    nn0p about 5 years
    How could you init a singleton object with arguments in a module?
  • joel
    joel almost 5 years
    should Foo be __Foo so that users won't casually just use that?
  • igorgue
    igorgue almost 4 years
    @nn0p pretty old, but you could introduce a third module config.py in which you can assign a variable: import c c.param = 10 import b b will import c and use c.param and it'll be 10, not ideal for some people but it works
  • theheadofabroom
    theheadofabroom over 3 years
    Pardon my ignorance, but why do you need to check twice for __singleton_instance? Could you not just always take __singleton_lock and then only check once?
  • Andrei R.
    Andrei R. over 3 years
    As I mentioned before, we need it to make sure that, while we are performing 'if' and using the lock, some other thread haven't created this instance already en.wikipedia.org/wiki/Double-checked_locking It's a fairly popular concept to be asked at interviews :)
  • theheadofabroom
    theheadofabroom over 3 years
    But surely the cost of acquiring an uncontested lock is low enough that if it were significant, you would be better off implementing it in C? IIRC the cost of acquiring the lock is about half that of a function call, and so the better optimisation here might be to avoid using the context manager and acquire the lock manually. If this is an unnecessary optimisation I'd argue double checking is even more so.
  • Andrei R.
    Andrei R. over 3 years
    Double checking is not an optimization, it is to make sure that we don't create two instances of a Singleton. It's also, good to point out that these checks will be executed only once, at the first initialization. After that it is just returning the instance. So any optimizations will be pointless.
  • theheadofabroom
    theheadofabroom over 3 years
    This is what I don't seem to be getting. Surely as long as you check while holding the lock you only need to check once? That's what the lock is there for, to synchronise access.
  • ThorSummoner
    ThorSummoner over 3 years
    @sleblanc I thought I had an SO post on the topic, couldn't find it; here is a top google result: stackoverflow.com/a/55910681/1695680 IIRC I needed this to monkey patch some incorrect behavior in python's stdlib's ssl certificate chain of trust assertions when multiple domains were used in a particular way, allowing some modules to have their ssl interfaces replaced with the monkeypatched version and others to not, and being able to swap them as needed. I do not recommend monkey patching, but I'm sure glad the option exists :)
  • Adam Acosta
    Adam Acosta over 3 years
    This should have been the accepted answer given the specifics of this question. Notably, this is exactly the way the actual Python standard library implements the logging module. See the code: github.com/python/cpython/blob/master/Lib/logging/__init__.p‌​y. And it's still configurable, which seems to be why the original asker didn't want globals initialized at module import time.
  • matanster
    matanster over 3 years
    This is a very ambiguous answer, may I suggest re-editing it completely in light of the discussion.
  • Sławomir Lenart
    Sławomir Lenart about 3 years
    if you are really looking for a one-line solution try a python's module as a singleton, which is actually a zero-line solution.
  • M Juckes
    M Juckes about 3 years
    I think you can get the same effect by defining a base class BaseSingleton(metaclass=Singleton) and then using Logger(BaseSingleton). One possible advantage is everything I need defined with BaseSingleton, rather than having both a base class and a metaclass. Does this make sense?
  • joanis
    joanis about 3 years
    This might work in some cases, but sometimes lazy initialization is important. In my use case, initialization costs 400ms, so I don't want to incur that just because I imported the module. It has to be incurred only when the singleton is really needed.
  • Alan Dyke
    Alan Dyke about 3 years
    @joanis. Agreed there is no perfect solution for every possible use case. Maybe, you can still use lazy initialization for the time consuming part of your code, by not putting it into your constructor. Or maybe you need one of the other more complicated suggestions on this page.
  • Ben Kovitz
    Ben Kovitz about 3 years
    A need to make this kind of singleton is what led me to this question. Much appreciated! I tried pip install handy-decorators and I get ERROR: Could not find a version that satisfies the requirement handy-decorators. Any suggestion?
  • joanis
    joanis about 3 years
    Indeed, I've opted for a metaclass-based solution. It did the trick for me. Although it seems to verbose to me. In C++, I've used X* theX = NULL; X* getTheX() {if (theX == NULL) theX = new X(); return theX;}. Not very pythonic, but quite a bit shorter. I might switch to that with None and if theX is None instead, for code that I feel will be easier to read in the future.
  • Ben Kovitz
    Ben Kovitz about 3 years
    I went ahead and copied the source code from here and decorated a dataclass. It worked the first time. Happily, it has no dependencies on any other code! Everything in that module is wonderfully simple and straightforward, truly Pythonic. If you aren't teaching Python, you should be.
  • Ben Kovitz
    Ben Kovitz about 3 years
    I'm giving this answer a –1 based on @GingerPlusPlus's comment. If you ever fix it, let me know and I'll remove the –1.
  • Inyoung Kim 김인영
    Inyoung Kim 김인영 about 3 years
    I found singleton useful when creating module for Syllogism. I mean python bool are already a singleton.
  • Luiz Felipe
    Luiz Felipe about 3 years
    Is double checking even needed for python ? that pattern is not about costs or optimization, but about memory model of the environment. The thing is, doesn't the GIL ensures proper locking when the object is being constructed ? There are languages in which the use of the double locking pattern is wrong, as there are reordering, besides, you don't always need it.
  • jossefaz
    jossefaz almost 3 years
    And what's happen when we call MyClass twice ? I got two different adress here...It does not seem to avoid new instances
  • Welsige
    Welsige over 2 years
    Would this solution be good for db connection handling? I would like to have just 1 place where my database connection is handled. My application uses multiprocessing with 100`s of threads that need to do batch executions in the database, so I am looking at ways to use resources more efficiently and thought about singletons, but not really sure they would work as intended in a multiprocessing app.
  • Alan Dyke
    Alan Dyke over 2 years
    @Welsige If you have 100s of threads you probably want 100s of connections to the database. Also, if you are using CPython, you should understand the implications of the Python GIL. I don't know your use case, but likely performance would be a problem in such a model.
  • polvoazul
    polvoazul over 2 years
    I've edited the answer to avoid this problem
  • Vinícius Queiroz
    Vinícius Queiroz over 2 years
    I imported the module in several different script files (all of which would use the "singleton"), with import ABC as X, and it was actually imported once for every script that had the import statement for the "singleton" module. So the variable I defined had its value reassigned each time the module was imported. I also tried removing the alias, and even importing it in every file of the project, same results. I'm on Python 3.9.6. Singleton with metaclass (from agf's answer) did the trick fantastically for my use case (logging, with a variable that must be initialized at compile time once)
  • Jonathan Komar
    Jonathan Komar over 2 years
    Caveat: The previous_instances dictionary of the implementation of @singleton does not look thread safe. If one thread is constructing an object while another object checks the dictionary, there is a race condition there...
  • Vijay Chavda
    Vijay Chavda over 2 years
    Haha, this is why they make memes on SO. The OP is asking how to do something but the answers are to not do it. :) I think it's awesome.
  • 00001H
    00001H over 2 years
    HEY!!!!!MAJOR CONS FOR METACLASS IMPLEMENTATION!!! If you do class MoreSingleton(Singleton,metaclass=OtherMeta) it will say TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases