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 signaled of some occurrence. for e.g imagine a Currency Converter which needs to output the converted currency in box2 while the user enters some value in the box1.
How does the box2 get to know that the user has entered something in the box1 and what needs to be done in the response to it
This is one of the most basic examples of the event based programming.
Another possible example could be HomeSecurity System where some possible actions such as the alarm need to be raised, the message needs to be sent to the owner and Police need to be informed about the possible theft are to be performed on the breach of the lock. These type 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.
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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks 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.
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- twitter-text-python (ttp) module - Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- XML parsing in Python
- Twitter Sentiment Analysis using Python
- pprint : Data pretty printer in Python
- Desktop Notifier in Python
- Junk File Organizer in Python
- Tracking bird migration using Python-3
- Python Desktop News Notifier in 20 lines
- Send mail with attachment from your Gmail account using Python
- Hangman Game in Python
- How to get synonyms/antonyms from NLTK WordNet in Python?
- Convert Text to Speech in Python
- Speech Recognition in Python using Google Speech API
- Birthday Reminder Application in Python
- Reading and Generating QR codes in Python using QRtools
- Performing Google Search using Python code
- Python | Plotting Google Map using gmplot package