Skip to content

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:

Callers route through one access point that creates and caches the instance on first request, then returns that same instance
One Access Point, One Shared Instance

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.

OOP in Python 3

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.

intermediate python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Aug. 11, 2026