• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 19. Events and Subscribers > Step 5: Writing an Event Subscriber

Step 5: Writing an Event Subscriber

Until now, you have not heard a word about events. But that is about to change because the next task is to implement the subscriber object. The generic event system is very simple: It consists of a list of subscribers and a notify(event) function. You can subscribe event subscribers to the event system by appending them to the list. To unsubscribe an object, you must remove it from the list. Subscribers do not have to be any special type of objects; they merely have to be callable. The notify(event) function takes an object (the event) as a parameter; it then iterates though the list of subscribers and calls each subscriber with the event as argument.

This means you have to implement a __call__(event) method as part of your message mailer API in order to make it a subscriber. The entire MessageMailer class, which you should put in the message module, should look like this:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint