Return to Snippet

Revision: 50165
at August 13, 2011 06:24 by fnl


Initial Code
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

Initial URL

                                

Initial Description

                                

Initial Title
Singleton class

Initial Tags

                                

Initial Language
Python