Tag Info

New answers tagged


I think the simplest, most Pythonic way to implement a singleton class is with inheritance: class Singleton(object): _instances = {} def __new__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = object.__new__(cls, *args, **kwargs) return cls._instances[cls] class Foo(Singleton): # Foo 'is a' ...


First and foremost, calling Singleton2's getMyObject() method will crash the program because it does not create a new instance when it is null, and will just return null. Second, there is no getMyObject in a singleton class. There is just getInstance().

Top 50 recent answers are included