One Shared Instance

Step 1: One Shared Instance

A Singleton guarantees a class has exactly one instance and gives everyone a single point of access to it. Config is the classic case: the whole app should read from and write to the same registry, not scattered copies.

Store the sole instance on the class itself in a private attribute, and hand it out through a classmethod.

Implement instance:

  • The first call to instance creates a Config and stores it on cls under _instance.
  • Every later call returns that same stored object, so identity holds across the app.

The reset classmethod is already written. It clears _instance so each test starts from a clean slate. The same instance living on the class would otherwise leak state from one test into the next.

Hints
Starter code
class Config:
    _instance = None

    def __init__(self):
        self._data = {}

    @classmethod
    def instance(cls):
        """Return the one shared Config instance, creating it on first call."""
        # TODO: if cls._instance is None create it, then always return cls._instance
        pass

    @classmethod
    def reset(cls):
        cls._instance = None
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.