Observer Pattern Tutorial: I NEVER knew events were THIS powerful 🚀
Video Statistics and Information
Channel: ArjanCodes
Views: 76,748
Rating: 4.9491739 out of 5
Keywords: observer pattern tutorial, observer pattern tutorialspoint, observer pattern, observer design pattern, design patterns, observer pattern python, design patterns through python, design pattern in python tutorial, python observer pattern example, observer design pattern python, listener design pattern, listener pattern, observer design pattern tutorial, design patterns tutorial, observer pattern explained, observer pattern python 3, observer pattern example, gang of four
Id: oNalXg67XEE
Channel Id: undefined
Length: 15min 16sec (916 seconds)
Published: Thu Feb 18 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
Finally a tutorial that teaches logic behind it not just do this do that and voila!
The fact that you've explicitly created a subscription model, makes this the Publisher-Subscriber pattern.
With the Observer pattern, observers must be aware of the existence of the observee, but that's not the case here. Instead, your functions have no knowledge of the handlers nor the handlers of the functions, they only know of a string being passed (published) to a central location.
Here are a couple SO posts about event systems in Python:
Difference between Observer, Pub/Sub, and Data Binding
Event system in Python
Note: an argument can be made that Pub-Sub is a variant of Observer, but the argument isn't a strong one
I don't completely agree this change improved the code a lot. Yes, the number of imports was reduced, but so now is reading the code more difficult—what happens at this point of code? How does it help in the overall task? Ultimately actually registering multiple event handlers in this kind of scenario would be only useful for debugging; admittedly it was nice how disabling adding the handlers disabled the function altogether.
There is still coupling: to understand what kind of parameters an event handler accepts, you need to find the site where the handler is defined, and the best way to do that is with
grep
, whereas with the original code theimport
directly tells you where the code is coming from. (I also enjoy usingmypy
so I guess I'm not a typical python developer; which, indeed, I don't write a lot of.)I find event handling best suited for code where the hooked function doesn't really expect or care anything in particular to happen for it to complete its job. So, for example if one part of the code would need to do setups when a new client connects or cleanups when the client disconnects; that's where I would use events.
...not to be confused with
threading.Event
orasyncio.Event
, which are also pretty powerful for synchronizing concurrent code.Python has a defaultdict. It would make the event.py file a lot easier:
While the observer pattern is very powerful, it must be mentioned that it also comes with a few drawbacks.
One drawback is that it is difficult to see what happens when an event is triggered. For instance: your program crashes when a new user is registered. How will you figure out which event listener caused the bug?
An other drawback is that the decoupling adds extra parts that can misbehave. For instance: No welcome email when a new user is registered. What is the cause? The new-user-function, the event-listener, the send-email-module, or was email not subscribed to register?
This was actually brilliant! I didn't really understand why you're doing it until you gave the example (switching from slack to teams) in the end. Subscribed! May i suggest, if you use an example in the beginning of why/how a technique would help that would make it even better. Either way, really enjoyed it!
This could probably be applied by a lot of people writing discord bots with discord.py
I learned a lot - thank you! Subscribed and liked.
Events are needlessly abstract and only make your code more difficult to understand /troubleshoot in the future. Just use threads and listeners.
CMV