Python design patterns

From wikinotes
Revision as of 22:40, 6 May 2018 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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__