/ Design Patterns / Creational patterns / Singleton Singleton Design Pattern in Python Back to Singleton description """ Ensure a class only has one instance, and provide a global point of access to it. """ class Singleton(type): """ Define an Instance operation that lets clients access its unique instance. """ def __init__(cls, name, bases, attrs, **kwargs): super().__init__(name, bases, attrs) cls._instance = None def __call__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super().__call__(*args, **kwargs) return cls._instance class MyClass(metaclass=Singleton): """ Example class. """ pass def main(): m1 = MyClass() m2 = MyClass() assert m1 is m2 if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...