Quickies

[categories] [index] [all (527)] [latest]

Python
  1. The following can be instanced several times, but all instances share the same state. Alex Martelli

    class Singleton:
        __shared_state = {}
        def __init__(self):
            self.__dict__ = self.__shared_state
    

    The following class returns the same instance for each instanciation.

    class Singleton(object):
        def __new__(type):
            if not '_the_instance' in type.__dict__:
                type._the_instance = object.__new__(type)
            return type._the_instance