The Subject

Step 1: The Subject

The Observer pattern has two roles: a Subject that holds a list of interested parties, and observers that want to be told when something happens. Everything reactive, event buses, signals, UI frameworks, is built on this one idea.

Here an observer is just a callable. Implement the broadcast.

  • notify(*args): call every subscribed observer with args, in subscription order
Hints
Starter code
class Subject:
    def __init__(self):
        self._observers = []

    def subscribe(self, observer):
        self._observers.append(observer)

    def notify(self, *args):
        """Call every observer with args, in subscription order."""
        # TODO:
        #   for observer in self._observers:
        #       observer(*args)
        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.