Is there a simple, elegant way to define singletons?
I don't really see the need, as a module with functions (and not a class) would serve well as a singleton. All its variables would be bound to the module, which could not be instantiated repeatedly anyway.
If you do wish to use a class, there is no way of creating private classes or private constructors in Python, so you can't protect against multiple instantiations, other than just via convention in use of your API. I would still just put methods in a module, and consider the module as the singleton.
Creating a singleton in Python
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 struct
s 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.
Why is this singleton implementation not thread safe?
I suggest you choose a better singleton implementation. The metaclass-based implementation is the most frequently used.
As for for thread-safety, neither your approach nor any of the ones suggested in the above link are thread safe: it is always possible that a thread reads that there is no existing instance and starts creating one, but another thread does the same before the first instance was stored.
You can use a with lock
controller to protect the __call__
method of a metaclass-based singleton class with a lock.
import threading
lock = threading.Lock()
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
with lock:
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class SingletonClass(metaclass=Singleton):
pass
As suggested by se7entyse7en, you can use a check-lock-check pattern. Since singletons are only created once, your only concern is that the creation of the initial instance must be locked. Although once this is done, retrieving the instance requires no lock at all. For that reason we accept the duplication of the check on the first call so that all further call do not even need to acquire the lock.
singleton design pattern in Python with state
This cannot work, because __init__
method is called after object creation through __new__
. Extract from Python Language Reference
If
__new__()
returns an instance of cls, then the new instance’s__init__()
method will be invoked like__init__(self[, ...])
, whereself
is the new instance and the remaining arguments are the same as were passed to__new__()
.
You should instead have a special initialization method distinct from __init__
and called at _instance
creation.
Code could be (no need for a parent class, so I omitted it):
class SingletonA:
_instance = None
def __init__(self):
# some state
print("Creating dummy state in SingletonA")
def _init(self):
# some state
print("Creating state in SingletonA")
self.X = 1.
self.Y = 2.
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(SingletonA, cls).__new__(
cls, *args, **kwargs)
cls._instance._init()
return cls._instance
But in fact, you can simply build the instance at declaration time:
class SingletonA:
def __init__(self):
# some state
print("Creating dummy state in SingletonA")
def _init(self):
# some state
print("Creating state in A")
self.X = 1.
self.Y = 2.
def __new__(cls, *args, **kwargs):
return cls._instance
SingletonA._instance = super(SingletonA, SingletonA).__new__(SingletonA)
SingletonA._instance._init()
Both ways would cause following output (for SingletonA part):
Creating state in A
Creating dummy state in SingletonA
Creating dummy state in SingletonA
('Same', 1.0, 2.0)
Completely removing the __init__
method would cause one single initialization.
The annotation version could be:
class ClassB:
def __init__(self):
# some state
print("Creating state in B")
self.X = 1.
self.Y = 2.
def singleton(cls):
obj = cls()
# Always return the same object
cls._instance = obj
cls.__new__ = staticmethod(lambda cls: cls._instance)
# Disable __init__
cls.__init__ = (lambda self: None)
return cls
@singleton
class SingletonB(ClassB):
pass
simply storing the singleton instance in the class itself
Singleton in go
Setting aside the argument of whether or not implementing the singleton pattern is a good idea, here's a possible implementation:
package singleton
type single struct {
O interface{};
}
var instantiated *single = nil
func New() *single {
if instantiated == nil {
instantiated = new(single);
}
return instantiated;
}
single
and instantiated
are private, but New()
is public. Thus, you can't directly instantiate single
without going through New()
, and it tracks the number of instantiations with the private boolean instantiated
. Adjust the definition of single
to taste.
However, as several others have noted, this is not thread-safe, unless you're only initializing your singleton in init()
. A better approach would be to leverage sync.Once
to do the hard work for you:
package singleton
import "sync"
type single struct {
O interface{};
}
var instantiated *single
var once sync.Once
func New() *single {
once.Do(func() {
instantiated = &single{}
})
return instantiated
}
See also, hasan j's suggestion of just thinking of a package as a singleton. And finally, do consider what others are suggesting: that singletons are often an indicator of a problematic implementation.
Related Topics
Prevent Scientific Notation in Matplotlib.Pyplot
How to Uninstall Python 2.7 on a MAC Os X 10.6.4
Why Is "Except: Pass" a Bad Programming Practice
Why Do Python Classes Inherit Object
How to Convert All Strings in a List of Lists to Integers
How to Get the Source Code of a Python Function
String Count With Overlapping Occurrences
Select Dataframe Rows Between Two Dates
C Function Called from Python Via Ctypes Returns Incorrect Value
Why Is _Init_() Always Called After _New_()
How to Get Line Count of a Large File Cheaply in Python
Meaning of @Classmethod and @Staticmethod For Beginner
How to Do Fuzzy Match Merge With Python Pandas
How to Safely Create a Nested Directory