How Singleton Works in Python Without Leaning on Strict Types
Some programming ideas sound heavier than they feel in Python. Singleton is one of them. In languages that lean hard on access modifiers and strict construction rules, the pattern can sound like a locked room: one class, one constructor path, one official instance. Python gets to the same destination with less ceremony.
That difference is what makes Singleton worth revisiting in Python specifically. The useful beginner question is not “How do I mimic a stricter language?” It is “How does Python actually express one-instance behavior?” Once that question is clear, duck typing stops sounding like a side note and starts sounding like part of the point.
This Is Not the General Singleton Article
The broad question of when Singleton helps belongs somewhere else. The implementation comparison belongs somewhere else too. This article has a narrower job. It is about what changes when Singleton lives inside a language that does not rely on strict type walls to communicate intent.
Python Solves the Core Problem Directly
In Python, Singleton usually begins in __new__(). That method decides whether a new
object should be created at all. If an instance already exists, the class can return it instead
of building another one.
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
obj_one = Singleton()
obj_two = Singleton()
print(obj_one is obj_two)
That is the important behavior. The class is not proving that it belongs to a formal Singleton hierarchy. It is simply behaving like a one-instance class.
Why Python Makes This Feel Lighter
In a stricter language, a Singleton often arrives with a private constructor and an official access method. Python does not need that same amount of ceremony to express the idea. The control point sits directly in object creation, and that keeps the mechanism visible.
That simplicity is useful for beginners because it reduces the chance of confusing the pattern’s ritual with its purpose. The purpose is one instance. Everything else is implementation detail.
Where Duck Typing Actually Enters
Duck typing does not mean different Singleton-shaped classes magically become the same object. It means surrounding code can often work with objects based on what they expose rather than on their formal labels. Two classes can both behave like singletons without sharing identity.
class AppConfig:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.mode = "production"
return cls._instance
class CacheConfig:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.mode = "memory"
return cls._instance
def show_mode(config):
print(config.mode)
show_mode(AppConfig())
show_mode(CacheConfig())
The function does not care what the class is called. It cares that the object has a
mode attribute. That is the Python angle. Behavior often matters more than the
label on the class.
What Duck Typing Does Not Mean
It does not mean separate classes collapse into one cross-class instance. If each class stores its own class-level instance, each class is still managing its own identity.
class SingletonA:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
class SingletonB:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
obj_a = SingletonA()
obj_b = SingletonB()
print(obj_a is obj_b)
That output is False, and it should be. Both classes are Singleton-shaped inside
their own definitions, but they do not share one universal object.
The Beginner Lesson Hidden Inside This
In Python, Singleton is less about proving membership in a pattern and more about controlling what happens when the class is called.
That is the cleanest way to hold it in your head. One class can guarantee one instance. Several classes can still be used similarly if they expose the same practical behavior. Those are related ideas, but they are not the same thing.
What a Beginner Should Keep
Python expresses Singleton through behavior at the object-creation point. Duck typing matters because surrounding code often cares more about what an object can do than about what the class officially claims to be. That gives Python a lighter, more direct version of the pattern.
What it does not give you is magical identity sharing across unrelated classes. One-instance behavior still belongs to the class that defines it.
Frequently Asked Questions
These are the practical questions people usually have when Singleton and duck typing start to overlap in Python.
What is the simplest Python Singleton idea?
The simplest idea is controlling instance creation so repeated calls to the class return the same object instead of making a new one.
Why does __new__() matter here?
Because __new__() decides whether a new object gets created at all. That makes it a natural place to enforce one-instance behavior.
Does Python need a private constructor for Singleton?
No. Python usually expresses the pattern with less ceremony. The important thing is controlling creation behavior, not copying stricter language rituals.
Does duck typing mean two singleton classes share the same object?
No. Duck typing is about how code uses objects based on behavior. It does not merge the identity of separate classes.
What does duck typing change in this discussion?
It changes how surrounding code can interact with singleton-like objects. A function can often work with different classes if they expose the same useful behavior.
Can two different singleton classes still be used the same way?
Yes. They can be used similarly if they expose the same methods or attributes, even though they still manage separate identities.
What is the biggest beginner confusion here?
The biggest confusion is mixing up object identity with shared interface. Singleton controls whether one class returns one object. Duck typing affects whether code can use different objects in similar ways.
What is the cleanest way to remember this?
Singleton is about one-instance behavior. Duck typing is about behavior-based use. They can complement each other, but they solve different problems.
Further Reading
If you want the broader “when should I use this?” question next, read When the Singleton Pattern Actually Helps .
If you want to compare two concrete implementations next, read Comparing Two Singleton Implementations in Python .
If you want the concurrency edge case next, read Why the Singleton Pattern Feels Simple Until It Doesn’t .
Comments