05 / 05

How would you implement the Singleton pattern in Python, and what are the caveats of doing so?

Difficulty: 8/10
Singleton, Design Patterns, Global State, Thread Safety

Singleton in Python: module-level state, new, or decorator, with caveats

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. In Python, the simplest and most Pythonic way to achieve this is a module-level instance, because modules are singletons by nature: they are imported once and cached in sys.modules. If you need a class-based singleton, you can override new to return a cached instance, use a metaclass to control instantiation, or use a decorator. Each approach has caveats. The pattern introduces global mutable state, which makes testing harder and hides dependencies. It can be thread-unsafe unless you add a lock. It complicates inheritance and can be broken by multiple interpreters or processes. In most cases, dependency injection is a better choice for testability and flexibility.

  1. 1

    Module-level instance: define a module with a single instance and import it. This is the most Pythonic and avoids metaclass complexity.

  2. 2

    Override new: check if an instance exists and return it. Must handle thread safety with a lock.

  3. 3

    Metaclass: override call to control instantiation. More complex but works with inheritance.

  4. 4

    Decorator: wrap the class to return a single instance. Simple but can break isinstance.

  5. 5

    Caveats: global mutable state, hard to test, hides dependencies, not thread-safe unless locked, and breaks with multiple interpreters or processes.

  6. 6

    Trade-off: singletons are convenient but often a design smell. Prefer dependency injection for testability.

  7. 7

    Common mistake: using a singleton for a database connection without pooling, which serializes access.

  8. 8

    Version note: Python's module system already provides singletons. No language-level singleton exists.

javascript

Scenario Questions

0-2 years experience

  1. 1What is a singleton and why might you want one?
  2. 2How do you create a module-level singleton in Python?

2-5 years experience

  1. 1You need a configuration object that is shared across your application. How do you implement it without a singleton?
  2. 2A singleton is causing test failures because state leaks between tests. How do you fix it?

5-8 years experience

  1. 1You have a singleton logger that is used everywhere. How do you make it testable?
  2. 2You need thread-safe lazy initialization for a singleton. How do you implement it?

8+ years experience

  1. 1Design a system that avoids singletons entirely while still providing shared configuration and resources.
  2. 2Compare the singleton pattern with dependency injection and module-level state on testability, concurrency, and maintainability.

Follow-up Questions

  • How would you test a class that depends on a singleton?
  • What is the difference between a singleton and a Borg (monostate) pattern?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.