The Voidspace Techie Blog

Gravatar Shouldn't the instance be kept on the 'self' variable and not the metaclass instance? Otherwise all users of the metaclass will reuse the same instance. In the metaclass 'self' should be renamed 'cls' or 'klass' to make the intent clearer.

class SingletonMeta(object):
__def __call__(cls):
____if getattr(cls, "instance", None) is None:
______cls.instance = type.__call__(cls)
____return cls.instance

And yes, singletons are usually an anti-pattern but like globals sometimes they're handy!


Gravatar You're right of course. I've changed the example, thanks.


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan