MIXINS in Python explained with an example
HTML-код
- Опубликовано: 7 дек 2024
- In this video you see an mix-in example Python. You learn what problems a mixin can solve and how multiple inheritance is used to achieve mixins in Python.
C H A P T E R S
0:09 Single inheritance
0:50 Adding behavior requires inheritance to prevent duplicate code
1:26 Inheritance violates single responsibility principle
2:05 Small addition affects whole system
2:20 Options to implement serialization functionality
2:35 Introduce mixin class
3:30 Use multiple inheritance to achieve mixins in Python
3:48 Difference between mixins and multiple inheritance
M Y O N L I N E T R A I N I N G
🎓Like my style of teaching? Try free chapters of my online courses: pythonforevery...
#pythonforeveryone #programming #python
short and to the point. great work
Thank you very much for the video.
You are welcome!
I think dependency injection to achieve the same thing without using inheritance. For example, I can assign self.serializer = Serializer() at def __init__(). Am I thinking correctly? When should I decide to use dependency injection vs mixins and vice versa?
Dependency injection is the technique to inject objects into other objects. Could it be you mean when to favor composition over inheritance? In this video, I show a scenario where DI and composition work very well: ruclips.net/video/BdMO1IR-800/видео.html
Great!
thanks!
I'm struggling with IDE typing errors when a mixin access to properties it doesn't posses (yet).
Any advice on how to handle that properly? Thank you!
Yep, that is a problem of dynamically typed languages. There is no guarantee that attributes will be present. If you want to satisfy the type checker, you could use base classes or protocols.
Can you make video abou SOLID principles?
Here is a youtube video: ruclips.net/video/VqeBgD5TuAQ/видео.html on the Open Closed Principle and if you want to support me, you can enroll in my S.O.L.I.D. course on Udemy: pythonforeveryone.com/python-solid-online-training.html
Allways use dark mode and big fonts.