Shared Read and Write

Step 2: Shared Read and Write

A registry is only useful if writes made anywhere are visible everywhere. Because instance always returns the same object, any value you store through one handle is instantly readable through another.

Implement two instance methods that operate on self._data:

  • set stores value under key.
  • get returns the stored value for key.

You do not need to worry about missing keys yet. That comes in the next step. Notice how the singleton makes shared state effortless: there is only one _data dict in the whole program.

Hints
Starter code
class Config:
    _instance = None

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

    @classmethod
    def instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    @classmethod
    def reset(cls):
        cls._instance = None

    def set(self, key, value):
        """Store key=value in the shared config data."""
        # TODO
        pass

    def get(self, key):
        """Return the stored value for key."""
        # TODO
        pass
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.