<s> from __future__ import absolute_import <EOL> import weakref <EOL> class Singleton ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( Singleton , self ) . __init__ ( * args , ** kw ) <EOL> self . __instance = None <EOL> def __call__ ( self , * args , ** kw ) : <EOL> if self . __instance is None : <EOL> self . __instance = super ( Singleton , self ) . __call__ ( * args , ** kw ) <EOL> return self . __instance <EOL> class Cached ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( Singleton , self ) . __init__ ( * args , ** kw ) <EOL> self . __cache = weakref . WeakValueDictionary ( ) <EOL> def __call__ ( self , * args ) : <EOL> if args in self . __cache : <EOL> return self . __cache [ args ] <EOL> obj = super ( Singleton , self ) . __call__ ( * args ) <EOL> self . __cache [ args ] = obj <EOL> return obj </s>
