/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Singleton(type): """Singleton metaclass. Do NOT inherit! Usage: class MyClass(object): __metaclass__ = Singleton ... """ def __init__(mcs, name, bases, dic): super(Singleton, mcs).__init__(name, bases, dic) mcs.__instance = None def __call__(mcs, *args, **kw): if mcs.__instance is None: mcs.__instance = super(Singleton, mcs ).__call__(*args, **kw) return mcs.__instance