Singleton Pattern in Python

I was looking for a smooth way to create singletons in python. Here is a
metaclass solution that seems to work:

class Singleton(type):
    def __init__(cls,name,bases,dic):
    def __call__(cls,*args,**kw):
        if cls.instance is None:
        return cls.instance

This was originally posted by Michele Simionato as a comment on a python
singleton recipe on

You use it like this:

class A( object ):
  __metaclass__ = Singleton
  # here goes the rest of you class definiton ..

But you knew that anyway, didn’t you? 😉