singleton
A singleton is a class restricted to a single instance, reached through one shared access point that returns the same object on every request. The same word names both the pattern and the single object it produces.
As one of the classic creational design patterns, a singleton enforces its own uniqueness rather than trusting callers to create only one object. A common implementation hides the class constructor and exposes a method that builds the instance lazily on first use, then returns the cached object thereafter. This access flow routes every caller to the same object:
The pattern suits resources meant to exist once per program, such as a logging facility, a configuration store, or a connection pool.
Because it ties code to one globally reachable object, the singleton draws frequent criticism. It threads hidden global state through a program, which complicates isolated testing and can become a bottleneck. Staying correct also demands careful locking when several threads request the instance at once.
Some languages make the pattern unnecessary by building single-instance behavior into the language itself. Python is one of them: a module is itself a singleton, because the import system caches it in sys.modules on first load and hands that same module object to every later import.
Related Resources
Tutorial
Object-Oriented Programming (OOP) in Python
In this tutorial, you'll learn all about object-oriented programming (OOP) in Python. You'll learn the basics of the OOP paradigm and cover concepts like classes and inheritance. You'll also see how to instantiate an object from a class.
For additional information on related topics, take a look at the following resources:
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Object-Oriented Programming in Python vs Java (Tutorial)
- Using and Creating Global Variables in Your Python Functions (Tutorial)
- A Conceptual Primer on OOP in Python (Course)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Python vs Java: Object Oriented Programming (Course)
- Working With Global Variables in Python Functions (Course)
- Using and Creating Global Variables in Your Python Functions (Quiz)
By Martin Breuss • Updated Aug. 11, 2026