'''singleton.py: A Python implementation of the Singleton pattern. Do not edit this file. It is extracted automatically from the documentation: http://www.nmt.edu/tcc/help/lang/python/examples/logscan/ ''' # - - - - - c l a s s S i n g l e t o n class Singleton(object): '''Base class for singleton objects. State/Invariants: Singleton.__classMap: [ a dictionary whose keys are the classes instantiated so far, and each related value is the single instance of that class ] ''' __classMap = {} # - - - S i n g l e t o n . _ _ n e w _ _ def __new__ ( cls, *args, **kw ): '''Instance factory. [ if cls is a key in Singleton.__classMap -> return the related value else -> Singleton.__classMap[cls] := a new object of class cls return that new object ] ''' #-- 1 -- try: return Singleton.__classMap[cls] except KeyError: inst = Singleton.__classMap[cls] = object.__new__(cls) return inst