On this page
article
Singleton Pattern
Ensure one instance of a class.
Category: patterns
Problem
Ensure one instance of a class.
Solution
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
Notes
- Adapt variable names and paths to your project
- Add error handling for production use
- See related chapters in the Learning Path