🤖📘🍻 Hooray! After 3 years of work, we've finally released a new ebook on design patterns! Check it out »

Observer Design Pattern in Python

Define a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updatedautomatically.

import abc

class Subject:
    Know its observers. Any number of Observer objects may observe a
    Send a notification to its observers when its state changes.

    def __init__(self):
        self._observers = set()
        self._subject_state = None

    def attach(self, observer):
        observer._subject = self

    def detach(self, observer):
        observer._subject = None

    def _notify(self):
        for observer in self._observers:

    def subject_state(self):
        return self._subject_state

    def subject_state(self, arg):
        self._subject_state = arg

class Observer(metaclass=abc.ABCMeta):
    Define an updating interface for objects that should be notified of
    changes in a subject.

    def __init__(self):
        self._subject = None
        self._observer_state = None

    def update(self, arg):

class ConcreteObserver(Observer):
    Implement the Observer updating interface to keep its state
    consistent with the subject's.
    Store state that should stay consistent with the subject's.

    def update(self, arg):
        self._observer_state = arg
        # ...

def main():
    subject = Subject()
    concrete_observer = ConcreteObserver()
    subject.subject_state = 123

if __name__ == "__main__":

Code examples

More info, diagrams and examples of the Observer design pattern you can find on our new resource Refactoring.Guru.