Observer method – Python Design Patterns

The observer method is a Behavioral design Pattern which allows you to define or create a subscription mechanism to send the notification to the multiple objects about any new event that happens to the object that they are observing. The subject is basically observed by multiple objects. The subject needs to be monitored and whenever there is a change in the subject, the observers are being notified about the change. This pattern defines one to Many dependencies between objects so that one object changes state, all of its dependents are notified and updated automatically.

Problem Without using Observer Method

Imagine you want to create a calculator application that has different features such as addition, subtraction, changing base of the numbers to hexadecimal, decimal, and many other features. But one of your friends is interested in changing the base of his favorite number to Octal base number and you are still developing the application. So, what could be the solution to it? Should your friend check the application daily just to get to know about the status? But don’t you think it would result in a lot of unnecessary visits to the application which were definitely not required. Or you may think about that each time you add the new feature and send the notification to each user. Is it OK? Sometimes yes but not every time. Might be some users get offended by a lot of unnecessary notifications which they really don’t want.

Solution using Observer Method

Let’s discuss the solution to the above-described problem. Here comes the object Subject into the limelight. But it also notifies the other objects also that’s why we generally call it Publisher. All the objects that want to track changes in the publisher’s state are called subscribers.







class Subject:
    """Represents what is being observed"""
    def __init__(self):
        """create an empty observer list"""
        self._observers = []
    def notify(self, modifier = None):
        """Alert the observers"""
        for observer in self._observers:
            if modifier != observer:
    def attach(self, observer):
        """If the observer is not in the list,
        append it into the list"""
        if observer not in self._observers:
    def detach(self, observer):
        """Remove the observer from the observer list"""
        except ValueError:
class Data(Subject):
    """monitor the object"""
    def __init__(self, name =''):
        Subject.__init__(self) = name
        self._data = 0
    def data(self):
        return self._data
    def data(self, value):
        self._data = value
class HexViewer:
    """updates the Hewviewer"""
    def update(self, subject):
        print('HexViewer: Subject {} has data 0x{:x}'.format(,
class OctalViewer:
    """updates the Octal viewer"""
    def update(self, subject):
        print('OctalViewer: Subject' + str( + 'has data '+str(oct(
class DecimalViewer:
    """updates the Decimal viewer"""
    def update(self, subject):
        print('DecimalViewer: Subject % s has data % d' % (,
"""main function"""
if __name__ == "__main__":
    """provide the data"""
    obj1 = Data('Data 1')
    obj2 = Data('Data 2')
    view1 = DecimalViewer()
    view2 = HexViewer()
    view3 = OctalViewer()
    obj2.attach(view3) = 10 = 15


Class Diagram

Following is the class diagram for the Observer Method




DecimalViewer: Subject Data 1 has data 10
HexViewer: Subject Data 1 has data 0xa
OctalViewer: SubjectData 1has data 0o12
DecimalViewer: Subject Data 2 has data 15
HexViewer: Subject Data 2 has data 0xf
OctalViewer: SubjectData 2has data 0o17


  • Open/Closed Principle: Introducing subscriber classes is much easier in Observer method as compared to others without making xhanges in the client’s code.
  • Establishes Relationships: Its really easy to establishes the relationships at the runtime between the objects.
  • Description: It carefully describes about the coupling present between the objects and the observer. Hence, there is no need to modify Subject to add or remove observers.


  • Memory Leakage: Memory leaks caused by Lapsed Listner Problem because of explicit register and unregistering of observers.
  • Random Notifications: All the subscribers present gets notification in the random order.
  • Risky Implementations: If the pattern is not implemented carefully, there are huge chances that you will end up with large complexity code.


  • Multi-Dependency: We should use this pattern when multiple objects are dependent on the state of one object as it provides a neat and well tested design for the same.
  • Getting Notifications: It is used in social media, RSS feeds, email subscription in which you have the option to follow or subscribe and you receive latest notification.
  • Reflections of Object: When we do not coupled the objects tightly, then the change of a state in one object must be reflected in another object.

Further Read – Observer Method in Java

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :


Please write to us at to report any issue with the above content.