[ Level 2 ] Test Singleton Implementation in Python.
Days ago, just search how to use implement "Singleton" design pattern. ( http://stackoverflow.com/questions/6760685/crea(ing-a-singleton-in-python ) And today, I just need to implement for it. In my case, I need to create mutliple loggers and I also want to use "Singleton" to reduce system resource usage. Therefore, I create a Singleton meta class and also use a parameter called "singleton_id" to define the instance category. Source Code: #!/bin/env python class Singleton(object): _singleton_key = 'singleton_id' _instance = {} def __new__(class_, *args, **kwargs): if class_._singleton_key not in kwargs.keys(): kwargs[class_._singleton_key] = '' if class_._singleton_key in kwargs and kwargs[class_._singleton_key] not in class_._instance.keys(): class_._instance[kwargs[class_._singleton_key]] = object.__new__(class_) return class_._instance[kwargs[class_._singleton_key]] class my...