First and foremost, What are events?
Events are the constructs that enable a class to notify other classes when something of interest takes place.
In the layman language, it is basically similar to raising a flag to signal others that something of interest has happened.
When to use events in Python?
Event-based programming is predominantly used while working with UI(user interface) where different components need to be signalled of some occurrence. for e.g imagine a Currency Converter that needs to output the converted currency in box2 while the user enters some value in box1.
How does box2 get to know that the user has entered something in box1 and what needs to be done in the response to it.
This is one of the most basic examples of event-based programming.
Another possible example could be the HomeSecurity System where some possible actions such as the alarm need to be raised, the message needs to be sent to the owner and the Police need to be informed about the possible theft are to be performed on the breach of the lock. These types of systems can be easily designed using the event-based mechanism where the event will be raised as soon as someone breaks the lock and in turn notifying the event handlers to play their part.
Now, in this post, we will use the latter example to understand how such events can be designed in Python where it is not supported by default.
Python3
class Event( object ): def __init__( self ): self .__eventhandlers = [] def __iadd__( self , handler): self .__eventhandlers.append(handler) return self def __isub__( self , handler): self .__eventhandlers.remove(handler) return self def __call__( self , * args, * * keywargs): for eventhandler in self .__eventhandlers: eventhandler( * args, * * keywargs) class Police( object ): def __init__( self , policeTelephoneNo): self ._telephone = policeTelephoneNo def CallPolice( self ): print ( "police have been informed" ) class Owner( object ): def __init__( self , ownerMobile): self .__mobile = ownerMobile def Message( self ): print ( "owner has been messaged about the possible theft" ) class Alarm( object ): def StartAlarm( self ): print ( "Alarm has started" ) # LockClass class Lock( object ): def __init__( self ): self .OnLockBroken = Event() def LockBroken( self ): # This function will be executed once a lock is broken and will # raise an event self .OnLockBroken() def AddSubscribersForLockBrokenEvent( self ,objMethod): self .OnLockBroken + = objMethod def RemoveSubscribersForLockBrokenEvent( self ,objMethod): self .OnLockBroken - = objMethod def Simulation(): # In the simulation we have a lock # which will be broken and the object of Police # owner and Alarm classes which are # to be notified as soon as lock is broke # Required objects godrejLockObj = Lock() localPoliceObj = Police( 100 ) ownerObj = Owner( 99999999 ) mainDoorAlarmObj = Alarm() # Setting these objects to receive the events from lock godrejLockObj.AddSubscribersForLockBrokenEvent(localPoliceObj.CallPolice) godrejLockObj.AddSubscribersForLockBrokenEvent(ownerObj.Message) godrejLockObj.AddSubscribersForLockBrokenEvent(mainDoorAlarmObj.StartAlarm) # Now the Lock is broken by some burglar # thus LockBroken function will be called godrejLockObj.LockBroken() # All three notifications must be printed # as soon as Lock is broken now # You can also remove any receiver by # calling the RemoveSubscribersForLockBrokenEvent godrejLockObj.RemoveSubscribersForLockBrokenEvent(mainDoorAlarmObj.StartAlarm) if __name__ = = "__main__" : Simulation() |
Output:
police have been informed owner has been messaged about the possible theft Alarm has started
The class that raises the event is called the publisher and the classes that receive the event are called subscribers.
Also, an event can have multiple subscribers and a subscriber can handle multiple events from multiple publishers
This article is contributed by Ankit Singh. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.