Python design patterns

From wikinotes

NOTE:

This page is just implementations.

See more general notes/best-practices in Algorithms: patterns .


Singleton

A singleton is a class that once instantiated always returns the same instance. This is useful for bundling global variables, or logs are a classic use.

class SomeSingleton( object ):
    __instance__ = None
    def __new__(self, *args,**kwargs):
        if SomeSingleton.__instance__ is None:
            SomeSingleton.__instance__ = object.__new__(self)
        return SomeSingleton.__instance__