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 its 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 its 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 its 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.
foo.x
or if you insistFoo.x
instead ofFoo().x
); use class attributes and static/class methods (Foo.x
). – Dianthe__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. – VexillumSingleton
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? – Pap__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 usetype
andsuper
instead ofobject
, 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. – Vexillumclass MyClass(threading.Thread, metaclass=Singleton):
– InfallibleMoreSingleton(Singleton,metaclass=OtherMeta)
it will sayTypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
– ColitisParameter 'self' unfilled
– FestusMyClass
becomes a function despite the fact thatgetinstance
returns a class? – Andriette_instances
mentioned in the class, isnt the one getting filled everytime a__call__
is made.cls._instances
creates an_instances
variable under the new class you made, and since there is only one instance, there will only every be one element in that for every class. Ideally it should have beenSingleton._instances
, which holds a map of all classes to their respective singleton instances. Doing it in the former way created problems during inheritance, but latter didnt. – Nazariodelete_instance
function on the metaclass, which will become a class method for the proper class, which can now be used to delete the instance. – Nazario