Protocol Or ABC In Python - When to Use Which One?

  Рет қаралды 195,061

ArjanCodes

ArjanCodes

Күн бұрын

When should you use protocol classes vs abstract base classes? Here's an example where I use both, talk about the trade-offs, and give you a suggestion of when to use each of them.
The code I worked on in this video is available here: github.com/ArjanCodes/2021-pr....
💡 Here's my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Designer Mindset Team Packages: www.arjancodes.com/sas
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
💬 Join my Discord server here: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
👀 Channel code reviewer board:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund
🔖 Chapters:
0:00 Intro
2:01 Explaining the example
5:56 About abstract base classes
8:47 Protocols, nominal typing and structural typing
10:41 Using protocols
15:34 Splitting the Device class
20:05 When to use protocols vs abstract base classes
#arjancodes #softwaredesign #python
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 288
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Get my FREE 7-step guide to help you consistently design great software here: arjancodes.com/designguide.
@dijakroot
@dijakroot 2 ай бұрын
Great content, Arjan. I've been trying downloading the design guide that you mentioned, but sadly the link from the email doesn't work.
@kyoungseoun
@kyoungseoun 2 жыл бұрын
Today, accidentally, I was searching "what is the difference between protocol and ABC" in google. And now I have this video! What a lucky day.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
I posted it just in time ;)
@atahirince
@atahirince 2 жыл бұрын
Big brother is watching you …
@__lasevix_
@__lasevix_ Жыл бұрын
So Google does use our data to improve our lives!
@Tekay37
@Tekay37 2 жыл бұрын
Dude, I am SO happy I found this channel a couple of days ago. These are exactly the kind of in depth discussions I've been searching for. It's the education I need at my skill level as a professional programmer. Thank you so much for doing these videos!
@EinsteinNewtonify
@EinsteinNewtonify Жыл бұрын
feels strange that I cannot find these info using just google.
@hoseynamiri
@hoseynamiri 9 ай бұрын
@ArjanCodes I need a book to study all these nice coding principles and unwritten rules of efficient python programming. Is there any reference out there?
@dantemendez3743
@dantemendez3743 2 жыл бұрын
The one advantage I see for protocols is cases where you might have overlapping function needs in multiple places which may be implemented by the same class. An example might be a tune up shop class that can perform services on anything with an engine that has a vehicle operator (car, motorcycle, tank, etc). Then, you have an entirely different class that is a tire shop class which can perform service on anything with tires that has a vehicle operator (car, motorcycle, bicycle, etc). In this case, a car can be serviced by the tune up shop or the tire shop but inheriting from both ABCs would cause a collision on the vehicle operator functions. On top of that, we can't have one combined ABC. Doing so would force the bicycle to implement all functions to meet the tune up shops needs and force the tank to implement all functions to support the tire shops needs, which wouldn't make sense since the bicycle can't go to the tune up shop. With protocols, these issues wouldn't arise because each shop would just require the things it needs in order to perform service.
@mateuszsmendowski2677
@mateuszsmendowski2677 Жыл бұрын
Literally the best channel about Python Software Engineering! Right place to gain theoretical knowledge, proper mind-set, and last but not least, the experienced developer's line of reasoning. Thanks, I firmly appreciate your work and contribution!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Mateusz, glad it was helpful!
@shashishekhar----
@shashishekhar---- Жыл бұрын
Yes
@owenonions2332
@owenonions2332 11 ай бұрын
Totally agree.
@rdean150
@rdean150 2 жыл бұрын
Protocols are exactly the sort of solution that is needed for proper interface declarations in large systems with complex interactions. Fantastic. Thanks!
@alessandroferrari2166
@alessandroferrari2166 2 жыл бұрын
this will be at position #1 on Google for protocol vs abc. No matter how many searches I have done, nothing explained so clearly like this video. Thank you, Arjan!
@rajiththennakoon7392
@rajiththennakoon7392 2 жыл бұрын
This channel became my go-to channel when I need some deep discussion. Excellent 🔥🔥🔥🔥
@imadetheuniverse4fun
@imadetheuniverse4fun 2 жыл бұрын
Another extremely informative video, thank you. Did not expect being able to split up Protocols like that, but it makes total sense. Will definitely be incorporating them more now.
@stephenalderman254
@stephenalderman254 2 жыл бұрын
One thing I find is often overlooked during the discussion of ABC vs Protocol, is that a Protocol is implemented as an ABC. (Technically, it's an extension of ABCMeta and ABC is just a helper class to simplify and keep code looking cleaner). Nothing prevents you from creating a class that extends Protocol and defining abstract methods as you would on an ABC - you will get the same behaviour and in fact is how the default protocols are defined within the typing module itself. This means depending on the exact use-case you can use either and/or both techniques within a single class definition.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Good point, Stephen!
@OmbraDiFenice
@OmbraDiFenice 2 жыл бұрын
this makes a lot of sense and it's actually what I was imagining from the point of view of implementing something similar to protocols in a language like C++: you could do it by redefining the same data structure in the various compilation units and then use this redefinition as a normal abstract base class. This also fits nicely with python's "same structure" concept - of course paying attention to padding if you're redefining it just partially or if you have more levels of inheritance. Maybe the parallelism is not quite correct, for example I feel that what @Vivek said about type traits is more technically precise (I'm not expert on traits), but it's a way to see things I guess :D
@matthiasschuster9505
@matthiasschuster9505 Жыл бұрын
@@ArjanCodes would you make a video on this?
@mrswats
@mrswats 2 жыл бұрын
The more I use mypy and typing in python the more I like it! It really helps me when writing functions or methods to know in advance what parameters I am expecting and what I do have to return. And, of course, sometimes catches logical errors, too, before running, which is great.
@TheMako101
@TheMako101 5 ай бұрын
A truly excellent video, as always, you've gone way above and beyond simply explaining the concept, and outlined excellent use cases and way of thinking to know when to pick which one. thanks again, Arjan :)
@ArjanCodes
@ArjanCodes 5 ай бұрын
I'm really glad my video was helpful!
@joel-eq8tq
@joel-eq8tq 2 жыл бұрын
Great explanation of the use cases of these two Python features. Really appreciate your channel and the clear way you go through the logic of your code and programme design.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you Joel, happy to hear you like the channel!
@brokenbe
@brokenbe 9 ай бұрын
Hi Arjan, glad to find this lecture, love it. Ty!
@nomex9829
@nomex9829 11 ай бұрын
I'll have to watch the video a couple more times, but it's brilliant stuff. You explained it so well I can't thank you enough.
@robertbrummayer4908
@robertbrummayer4908 2 жыл бұрын
Hi Arjan! Thank you for this excellent video and example. I think that you need multiple protocols in this example is a symptom of violating the single responsibility principle in the device class in the first place. The device class clearly has more than one responsibility. It actually reminds me of Uncle Bob's modem example where he shows how single responsibility violation looks like. The modem has the functionality "connect" and "disconnect" (responsibility 1) and "send" and "receive" data (responsibility 2) if I remember correctly. I think this example can be found in his book about agile software development. Anyway, great job!
@Ziggity
@Ziggity 2 жыл бұрын
I was literally debating with myself which of the two will be more suitable for a project I have at work when I opened KZbin. Impeccable timing and excellent video 👌🏽
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Happy to serve 😊
@Archfile375
@Archfile375 Жыл бұрын
Very interesting, this is exactly a paradigm for a driver collection/service that I hacked very poorly together to test a piece of hardware with the intention of wrapping that driver up in a connection management service. This a most fascinating look at a much better way of handling my idea. Thank you very much for the work, not just because it hit on a topic that was directly interesting to me but because you've done such a great job of making videos on non-trivial topics in general
@quiagonjin108
@quiagonjin108 2 жыл бұрын
Your video finally made it clear how these two approaches differ, and why protocols are often the better choice. Thanks!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome, glad it was helpful!
@marcopasopas
@marcopasopas Жыл бұрын
Thanks very much for the clear explanation! I was searching for the difference between ABC and Protocols but was unable to find such a clear explanation!
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad it was helpful!
@IgorRibeiroBraga
@IgorRibeiroBraga 2 жыл бұрын
Great explanation. Very good work! Congrats!!
@Nobody313
@Nobody313 Жыл бұрын
I love duck typing filosofy/pattern. It keeps the things so simple and organic.
@thallysrc
@thallysrc 2 жыл бұрын
best channel with the best content available with the best coder
@ohandyya
@ohandyya 2 жыл бұрын
Thanks for creating and sharing this video. This is a topic that confused me for quite some time.Your explanation is very clear. Love it.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad to hear it helped, Andy!
@shilinwang1847
@shilinwang1847 Жыл бұрын
crystal clear, thanks for the video!
@fluidmarble
@fluidmarble 2 жыл бұрын
Arjan, I love your channel. thank you so much for the high quality. I have no doubt your channel will be recommended in CS courses within a year
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you, glad you like it!
@Pawl0solidus
@Pawl0solidus Жыл бұрын
Protocols seem to be a great way to achieve the Interface Segregation Principle of SOLID. Probably I'm going to use it more instead of only relying on ABCs. Thanks for the amazing video!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Paulo, happy you’re enjoying the content!
@hoseynamiri
@hoseynamiri 9 ай бұрын
​@@ArjanCodesI need a book to study all these nice coding principles and unwritten rules of efficient python programming. Is there any reference out there?
@matiaszulberti9996
@matiaszulberti9996 2 жыл бұрын
Thank you so much! The explanation is so clear and the content super useful.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad you liked it, Matias!
@josephlyons3393
@josephlyons3393 2 жыл бұрын
Always enjoy your videos. Would love a video on how you personally set up each of your python projects within VS Code.
@ddctechinstitute6861
@ddctechinstitute6861 Жыл бұрын
@ArjanCodes
@WiktorWandachowicz
@WiktorWandachowicz 2 ай бұрын
Edit: I wrote my comment before playing the video up to the end, where Arjan mentions both ISP and Uncle Bob. But I decided to leave the comment as is. Anyway, good job Arjan! What happens here when Device abstract base class was replaced by two protocols seems like "I." from "S.O.L.I.D." principles. Namely "ISP = Interface Segregation Principle". And hey, Python allows doing that without too much strict typing, but rather with "duck typing"! Depending on the case this may be handy. And sometimes when more info is necessary when implementing a new class (to avoid mistakes ofc.) - would be to stick to ABC. Anyway, thanks for informative video 👍
@almoni127
@almoni127 2 жыл бұрын
Thanks for the video! For me python's dynamic typing seems as a major design flaw, and ABC and protocols look like convoluted afterthoughts.
@69AndyM
@69AndyM 2 жыл бұрын
Love this video. Thanks for touching on the differences between the two and showing examples. :) Love your Python videos. I code primarily in Python and in Golang. Wish there was a teacher like you for golang! It's an untapped market imo.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks Andrew, glad you like the videos! I’m not a Go expert (yet ;) ), but I’m intrigued by it.
@endab5517
@endab5517 2 жыл бұрын
This video brought attention to the letter board for this new subscribers. Now we need to go back to find your hard work! Thank you for the great content. Take care
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you, glad you like the videos!
@smann43231816
@smann43231816 2 жыл бұрын
Another great tutorial. Thanks again
@uwegenosdude
@uwegenosdude 3 ай бұрын
Thanks again for your very helpful video which explains the differences between protocols and ABCs.
@ArjanCodes
@ArjanCodes 3 ай бұрын
Glad the content was helpful!
@juanjoseexpositogonzalez1126
@juanjoseexpositogonzalez1126 11 ай бұрын
Really good explained. It's given me some good piece of advice for an app I'm refactoring.
@patricktarnaud
@patricktarnaud 2 жыл бұрын
Great explication ! Thanks
@JoaoSantos-lv4rc
@JoaoSantos-lv4rc Жыл бұрын
listened to this again for lunch and already applied protocols succesfully for the first time:)). thanks again Arjan.
@ArjanCodes
@ArjanCodes Жыл бұрын
Great to hear!
@rungxanh2901
@rungxanh2901 2 жыл бұрын
Thank you very much Aarjan! This is something college never teaches me 🤩
@hassaanalansary
@hassaanalansary 2 жыл бұрын
I think Protocol is meant to define an interface and use it only as type hinting for arguments. Similar to Iterable, if an object has a couple of dunder methods, it means it adheres to the Protocols ABC should be used to define relationships between superclasses and subclasses as mentioned in the of the video
@cheaterman49
@cheaterman49 2 жыл бұрын
I agree, and it goes beyond that IMHO ; sometimes it is impossible to accurately define a function with pure type hinting (say when a method takes a coroutine as argument), and you can instead implement a Protocol's `__call__` method to accurately describe it.
@sirmidor
@sirmidor Жыл бұрын
@@cheaterman49 >(say when a method takes a coroutine as argument) Just use typing.Coroutine (can be parameterized too)?
@SeanCallahan52
@SeanCallahan52 2 жыл бұрын
If your videos are anything like your code irl, it must be thumbs up.
@sinavski_com
@sinavski_com Жыл бұрын
Great video as always, Arjan! Although I would argue for a stronger conclusion: I feel that abc IS obsolete. First, you totally can explicitly use protocols as a base class (class Animal(Protocol): ... ; class Cat(Animal): ...). And second, you can even have a default method implementation in a protocol (although, I strongly advise against it). With that, abc doesn't seem to have any advantages in practice. I switched to protocols completely and don't regret it (more details if you google “sinavski abc vs protocols”).
@EvenTheDogAgrees
@EvenTheDogAgrees 5 ай бұрын
Nice article, but relies a bit too much on external resources for context, rather than providing the context directly. External resources are prone to link rot, robbing the reader of your article of the required context. They also require a context switch: the reader needs to open the link and read the (often lengthy) article, which probably covers far more than the limited coverage that understanding of yours requires. Hopefully it doesn't also reference external resources of its own, or the reader will have absorbed dozens of overlapping articles before they're ready to return to yours. And then the reader needs to come back to your article, offload the discussion from the external article, and pick up the train of thought that was interrupted when they were sent off elsewhere. I think it's better if information is self-contained. Referencing external resources for a more in-depth treatment of the context is OK, but the parts that are necessary for a proper understanding of the primary article should be _in_ the primary article, even if in shortened form and lacking otherwise important nuance that just isn't relevant in the context of the primary article. Also, links where the anchor text is just "here" are useless once the link target disappears. The reader has no idea what the title was, who the author was, and will be unable to retrieve the information even if it did survive elsewhere, as they have zero clues on what to search for. And that's something I just realised I should also start applying to my own writings, as I tend to do the same. 😂 Anyway, don't take this as a "you suck" criticism; it is meant in a constructive way. If I thought your blog post was not worth my time, or if I didn't appreciate your effort, I wouldn't have bothered responding. I love it when people freely share their knowledge, moreso when they're knowledgeable about the subject and cover things that go beyond the introductory matter. Your blog post was certainly appreciated.
@joem8251
@joem8251 2 жыл бұрын
Thank you for presenting this topic! Last week I spent more time than 23:45 trying to figure this out.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome Joe, glad you liked it! I also spent way more time than 23:45 to figure it out before I recorded the video, so I totally get it 😊.
@greob
@greob 3 ай бұрын
Great demonstration, thanks for sharing!
@ArjanCodes
@ArjanCodes 3 ай бұрын
I'm glad to hear you enjoyed the video! Thank you for the comment.
@JoaoSantos-lv4rc
@JoaoSantos-lv4rc 2 жыл бұрын
i have to admit i hadn't noticed the letterboard.. i'm sorry! love your content. it's really helped me stay motivated. And helped my coding, details and structre. thank you so much.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 2 жыл бұрын
Protocol seems more suited for distinguishing _traits_ with _trait objects_ . For example, you might define an interface that's really just an extension for objects like BeautyPrint with one method beauty_print() that just beautifully prints the object. Now, this isn't really meant to establish a hierarchy of beautifully printed objects so much as it gives an extension methods to those who need it. I don't think anything useful comes out of defining a function that takes a BeautyPrint. That is, BeautyPrint might be a _trait_ (interface), but it's not a _trait object_ (doesn't represent a bunch of polymorphic types). Protocol might help prevent it from being a _trait object_ if you so choose.
@rajatsethi
@rajatsethi 2 жыл бұрын
Other newer languages have this feature like Rust
@andydudley6085
@andydudley6085 2 жыл бұрын
It's nice to see your vim usage getting better over time. Should be able to `da(` without actually having the paren selected, btw. Then you could hit `.` to repeat the command. And if you used / to find the open parent, you could then `;.;.;.;.` to repeatedly find and delete the class inheritance syntax.
@vxsniffer
@vxsniffer 2 жыл бұрын
most of Python tutorials are basics... your channel is very pleasant discovery ;-)
@a1g2r3c4
@a1g2r3c4 Жыл бұрын
Great explanation, you saved the day
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you Alejando, glad you liked the video!
@Hubert4515
@Hubert4515 2 жыл бұрын
thanks, i've been using these ones a lot lately!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome!
@this-is-bioman
@this-is-bioman Жыл бұрын
This is an excellent example! Without foos and bars 🤩
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much @bioman!
@engdoretto
@engdoretto 2 жыл бұрын
Thanks for the awesome explanation!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome Carlos!
@ninobach7456
@ninobach7456 5 ай бұрын
The part I found the most impressing was how you used your IDE to edit the code, mostly with shortcuts.
@ArjanCodes
@ArjanCodes 5 ай бұрын
Glad you liked it!
@jaortizco
@jaortizco Жыл бұрын
Great content, Arjan. I'm learning a lot from your videos. I'd like to ask a question though. What should I do if I want to support different device objects from different external libraries? Let's say, for example, that they all implement a connection method but with different names, like "connection()" or "connect_to()".
@fnarsiste
@fnarsiste Жыл бұрын
Hello Arjan. Thank you for the quality of your videos and all the knowledge that you dissipate. This video is from a year ago but you look younger (I prefer you old [😎])
@Michael201078
@Michael201078 Жыл бұрын
The best channel for Python design principles.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you Michael!
@hotlinkster123
@hotlinkster123 2 жыл бұрын
I'm still not convinced protocols are better than ABC's when used in the way shown in the video. I will address the 2 main points: 1) Removing inheritance and imports: This removes the explicit relationship between the parts of code, and turns it implicit. You say it reduces coupling; yes in the actual program, but it only moves the coupling into the brain of the programmer, as they have to know about which protocols the class should implement. I find this bad because then you are relying on the programmer to ensure the protocol is correctly followed, and computers are better at catching errors than people. 2) Splitting up the protocol and multiple inheritance: Splitting up the interface can be done with ABC's, but this is supposed to be bad because multiple inheritance is then needed. I don't see a problem with multiple inheritance though. Sure it can get messy quick if the defined ABC's are bad, but the same is also true for protocols. Consider functions for a second; should we opt for global variables if we need more than one input variable? It "reduces coupling" because when it is called you don't need to know anything about the function or the input variables. I feel that many people would disagree with using functions like this. I will say however, that the points made from 20:59 are good use cases for protocols, and are the most compelling reasons I've heard for using them
@Forseti2
@Forseti2 2 жыл бұрын
Exactly my thoughts!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Good points!
@sebastianrodriguezcolina634
@sebastianrodriguezcolina634 2 жыл бұрын
I strongly disagree with (1), the programmer doesn't need to remember the interface, it is done by the type checker. With (2) I disagree just a bit, because you could use functools.partial and closures to remove the need for global variables.
@Forseti2
@Forseti2 2 жыл бұрын
@@sebastianrodriguezcolina634 yop, the type checker will do its job, but imagine you'll work in team of several people (some will quit the job, some new will arrive). When using ABCs it's always clear what "interface" the class implements. When using Protocol it's not so clear at first glance. The type checker or python interpreter gaves you hint eventually, but otherwise you must keep track of the interface implementations somehow (in your head when working a alone, in some shared documentation when working in the team)
@RichardVodden1
@RichardVodden1 2 жыл бұрын
I think of Protocols and ABCs as changing the ownership of the definition. ABCs are owned by the implementation, the person creating the class has to know, at the time the class is created, that they want to implement that ABC - it reduces the complexity of implementing consumers as you can be quite rigid in your ABC. A Protocol on the other hand is a declaration by the consumer of what they're able to consume. A Printer class may be able to consume any object which has a `print` method which returns an `str`, and therefore it defines a `Printable` protocol. The authors of the classes which the Print class consumes didn't have to realise that they were implementing the protocol. In both cases the method implementation has to do the right thing, and no typing mechanism is going to help assert functionality beyond the correct parameter and return types.
@danilob2b2
@danilob2b2 2 жыл бұрын
Arjan... I really enjoy your videos. Thank you for sharing your knowledge! Would you consider doing a video about Mixins? I've seen it is largely used in Django framework, and I would like to know more about best practices about Mixins.
@matthiasschuster9505
@matthiasschuster9505 Жыл бұрын
He says in a different video to not use them
@Antuan2911
@Antuan2911 Жыл бұрын
Hi Arjan, this is an amazing useful video for beginners like me. Thank you!!! May I ask a silly question. A device class must have exactly the same methods of the Device(Protocol) class? Or just must have at least the methods protocol describes? I mean, can a device class has more methods than protocol needs? Thank you in advance!
@tir0__
@tir0__ Жыл бұрын
What an amazing way of explanation
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad you liked it!
@kevon217
@kevon217 Жыл бұрын
Your videos never disappoint!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks!
@joningram
@joningram 2 жыл бұрын
Interesting video - Protocols seem like a good way to formalise duck typing and effectively introduce interfaces into Python by the back door. It might even get me using typing more! The only thing I don't like about them is the ... notation - this joins things like dataclasses (when is something a class variable and when is it an instance variable?) and properties in stretching the syntax of classes in ways that get hard to explain to beginners. I've even read the Protocol PEP before but it didn't click with me that ... was actual syntax rather than just shorthand in their examples for code they hadn't written!
@joningram
@joningram 2 жыл бұрын
... and after posting that I've now read several articles about ... - I didn't realise it had such a long Python history (introduced as the singleton Ellipsis in 3.0). Perhaps because I don't use numpy, which seems to be the only place it was used before typing and protocols. I like the anecdote that it was introduced as a constant because they thought it looked 'cute'!
@Marv_Stuff
@Marv_Stuff Жыл бұрын
I watched this video now a few times and it is really great. Now I have a question relying on the Interfaces: How would I visualize the optional diagnostic Interface in the UML (for example in mermaid)? I am on my way to understand design patterns and principles but this would be good to know for me. I also ask myself how to keep track of the different protocols in large projects (in my job we have a Django application with many….many apps)? If the question is too complex to answer maybe an idea for an advanced mermaid tutorial? ❤ Thank you for your great videos!!!
@oleholeynikov8659
@oleholeynikov8659 5 ай бұрын
Thanks, great video! What about Sphinx dealing with Protocol?
@adjbutler
@adjbutler 2 жыл бұрын
Thank you for this video once again.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome!
@carlwestman9343
@carlwestman9343 2 жыл бұрын
Nice videos! What was the shortcut called which you used to directly remove the inheritance but not the : at around 12:00? 😉
@krissn8111
@krissn8111 Жыл бұрын
Great video. But i have a question about HueLight as protocol how is it different from any other class? I wonder if the inheritance from Device was correct if we do not expect the abstractmethods from Device to be mandatory for it.
@rdean150
@rdean150 2 жыл бұрын
Is there a way to declare that your class satisfies a Protocol, for the sake of future developer groking the design? I guess it would require an import of the Protocol class / schema. I guess that may just be where naming convention or documentation comes into play?
@andrewglick6279
@andrewglick6279 2 жыл бұрын
It looks like there is a lot of benefit to using Protocols over ABC, however, I still _really_ want to see Python supporting some way of explicitly defining that a class implements a Protocol, similar to TypeScript's Interfaces (I believe you've mentioned this in a previous video; it is still just as valid). Also, I'd (personally) like to see type checkers raise warnings about missing methods/attributes from ABCs/Protocols at a class' definition instead of at the class' usage elsewhere in the code. If I forget to return a value in a statically-typed method, I expect to get a warning in the method definition, not when I attempt to call the method elsewhere in the code. There may be implementation issues for classes using methods from multiple files (which I've only ever read about), but I'm sure that's its own can of worms when it comes to best practices.
@Naej7
@Naej7 Жыл бұрын
I wanted to write the same thing
@Golgafrincham
@Golgafrincham 2 жыл бұрын
Do you use some kind of plugin or extension for the highlighting? I cloned your repo and commented out the "connect"-method in Huelight as you do but I do not get any notice at all that the HueLight is not following the expected Protocol. I only get an error when running saying: "AttributeError: 'HueLightDevice' object has no attribute 'connect'"🤔
@fernandocorenstein6531
@fernandocorenstein6531 Жыл бұрын
Best as always. Super Pro
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Fernando, glad you liked it!
@ice7mayu
@ice7mayu 2 жыл бұрын
Great tutorial. Thank you very much
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad you like it!
@courage396
@courage396 4 күн бұрын
Great tutorial, quick question what font are you using on your IDE
@rrwoodyt
@rrwoodyt 2 жыл бұрын
Excellent overview of ABCs vs Protocols. I miss the white board though-- it was one of my favourite parts of your videos.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks! Who knows, perhaps the letterboard makes a return in the future 😎
@ThePaulWilliams
@ThePaulWilliams 2 жыл бұрын
@@ArjanCodes When the one-liners come easy you should slip it back in just to see if we're paying attention. :)
@danielklaffmo4506
@danielklaffmo4506 2 жыл бұрын
Hey all cool, thank you so much for teaching us more than happy to give you the views, main focus is programming so hey thank you Arjaan I love the Netherlands btw tiptop thumbs up from a Norwegian
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi Daniel, thank you and glad you like the videos! Haven’t been to Norway, but it’s a place I’d love to visit one day.
@clauseclause6640
@clauseclause6640 2 жыл бұрын
You can achieve the same behavior with ABC just by using the register or subclasshook methods. The main downside of ABC is that you can create an instance of the abstract class, so I'm using Protocol instead, with abstractmethods and inheritance, and that works for me.
@ari_archer
@ari_archer Жыл бұрын
This one's really helpful
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Ari, Glad that the content it helpful
@joshdotmp4
@joshdotmp4 2 жыл бұрын
Love your videos, this clarified a lot of things I didn't understand about protocols. They seem to be great for splitting up coupling and being sure each part of the program only knows exactly what it needs. I have a question, though: Do protocols provide things other than type hinting? Can they raise errors or prevent vulnerabilities in other ways? I was surprised that Python didn't raise an error when you run register_device, for instance, until it actually ran into that missing connect method. Though the type hinting is very helpful in an IDE. Thanks!
@isodoublet
@isodoublet 5 ай бұрын
Type hints are glorified comments, the interpreter won't check any of them.
@and4828
@and4828 2 жыл бұрын
Another very interesting video, thanks!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad you liked it!
@moneycrab
@moneycrab 2 жыл бұрын
So good 😊, such clear education
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad it was helpful!
@stifstifler
@stifstifler Жыл бұрын
Great video! very insightful I have a question: is there any problem if the same method have different arguments on different subclasses of a protocol? or should I be sticked to use the same arguments?
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks! Regarding your question: I would definitely avoid that, otherwise what would be the purpose of defining types if the subclass doesn't adhere to it?
@stifstifler
@stifstifler Жыл бұрын
@@ArjanCodes it is a “player” class (the protocol one) and for a “human” (subclass) being one method uses the UI to handle human input but for the “bot” subclass that exact method requires some data that passess through the method but not the UI… I’m just learning about OOP and I’m designing simple games onto which I want to try all those new things.
@zacky7862
@zacky7862 2 жыл бұрын
Finally this is what I've been waiting to fully understand the use and importance of abstract and protocol. What error highlighting you are using? I'm using pylint but it's not showing like that specially on unused imports
@ArjanCodes
@ArjanCodes 2 жыл бұрын
I’m using the Python extension in VS Code (Pylance). To get the type checking messages, make sure that typeCheckingMode in your settings is set to ‘strict’. See also: code.visualstudio.com/docs/python/settings-reference.
@zacky7862
@zacky7862 2 жыл бұрын
@@ArjanCodes Oh thank you so much.
@zacky7862
@zacky7862 2 жыл бұрын
@@ArjanCodes Thank you it's now working.
@hansenmarc
@hansenmarc Жыл бұрын
Great stuff! Protocol seems like a nice match for using mixins. I’d love to hear your thoughts on the proper use of the mixin design pattern (for example, in developing scimitar-learn transformers or estimators).
@matthiasschuster9505
@matthiasschuster9505 Жыл бұрын
He says not to use Mixins at all You can see this at the end of one of his videos on dataclasses They actually break slots on dataclasses
@hansenmarc
@hansenmarc Жыл бұрын
@@matthiasschuster9505 Thanks! That’s very interesting. The scikit-learn library makes heavy use of mixins. I’ll have to watch the video you mentioned: kzbin.info/www/bejne/fKDXZJJvmqmghdU
@TJ-hs1qm
@TJ-hs1qm Ай бұрын
Protocols are mirrored by Type Classes in functionsl programing Type classes are a powerful feature in functional programming that can help solve the expression problem of adding new types to which existing operations apply. In functional programming, the expression problem refers to the challenge of adding new functionality to existing code without modifying that code. This is particularly difficult when dealing with types, as adding a new type often requires modifying existing code to support it. Type classes provide a solution to this problem by allowing you to define a set of operations that can be applied to any type that implements the type class. This means that you can add new types to your code without modifying existing code, as long as those types implement the required operations. For example, let's say you have a type class called `Printable` that defines a single operation called `print`. Any type that implements the `Printable` type class can be printed using the `print` operation. Now, if you want to add a new type to your code that can be printed, you simply need to implement the `Printable` type class for that type. You don't need to modify any existing code that uses the `print` operation.
@MrDivar
@MrDivar 10 ай бұрын
Hi @arjanCodes, I'm curious to know what course you provide includes the amazing video, Protocol Or ABC In Python - When to use which one?, I just watched above. Is this part of "The Software Designer Mindset" course?
@ArjanCodes
@ArjanCodes 10 ай бұрын
Hi! it's covered in The Software Designer Mindset. :)
@raameeil
@raameeil 7 ай бұрын
תודה!
@ArjanCodes
@ArjanCodes 7 ай бұрын
Thank you!
@DS-tj2tu
@DS-tj2tu 2 жыл бұрын
Thank you
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You're welcome!
@omerorhan80
@omerorhan80 2 жыл бұрын
Can you do a DDD and event sourcing example with KAFKA in python please? There is not much samples around with python.
@lixzhou1140
@lixzhou1140 2 жыл бұрын
What's a correct way to specify a multi-protocol constrained parameter?
@bingebinge3722
@bingebinge3722 Жыл бұрын
It is funny to see how strongly Python claim to be a pure OOP language, and being slow at enhancing its OOP features. Great content and great channel!
@liquiddddd
@liquiddddd 7 ай бұрын
Why multiple inheritances would be required if instead of protocol one decides to use Abstract Base Classes? Couldn't I just have the same 2 separate ABC classes (Device & DiagnisticSource) and then simply have their own concrete subclasses and use objects of those concrete subclasses as you did there?
@banatibor83
@banatibor83 Жыл бұрын
I took me a couple of minutes but I got it. Protocols are like interfaces in Java. Except in Java you have to explicitly implement an interface on an object. In python if the class has certain methods it can fulfill a protocol aka implements an interface but nothing tells the poor developer that a class actually realizes a protocol. It looks like a bunch of unrelated objects which somehow work together. The correct way would be IMO in this example to define two ABCs, Device and DiagnosticSource and a specific device should inherit from both. That way the connection would remain clear between the classes yet anything could inherit from one or the other. As I see with Protocol the code could be simpler at first glance but in reality it become more complicated and much more harder to read. Other problem is if a class has a certain method it fulfills a protocol when that class is never meant to do that. In the example, lets say a message queue adapter easily can pass as a device cause it probably has connect, disconnect and send_message methods. Protocol is a shady stuff!
@isodoublet
@isodoublet 5 ай бұрын
There's nothing wrong with multiple inheritance and this is precisely the kind of situation where it's useful. The decision between structural and nominal typing should be one of interoperability: do you want classes that implement the required interface to work with your code, or would you like to keep things controlled and open access only to those that explicitly opt in? You may want different things in different cases.
@preadaptation
@preadaptation 6 ай бұрын
Thanks
@ArjanCodes
@ArjanCodes 6 ай бұрын
Thank you for the support! Glad you enjoyed the content :)
@anson2416
@anson2416 2 жыл бұрын
Hi thanks for the vedio. May I know how do you do the following via shortcut which is quite useful? Thanks! 1. Replace pass with 3 dots ... 2. delete (device) in function.
@Julie9009
@Julie9009 Жыл бұрын
I suspect that Arian is using a ‘vi’ type plugin. In vi, to change a word, you simply put the cursor at the start of the word and type ‘cw’ and the new word
@ponysmallhorse
@ponysmallhorse 2 жыл бұрын
So, Protocols is "Golang" interfaces principles ported to python? Am i getting this correctly?
@davidkaftan5563
@davidkaftan5563 2 жыл бұрын
What type checker are you using in vs code? I am using PyLance, but yours seems to be more thorough. Great vids, much appreciated!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi David, I also use Pylance, but you have to make sure that your typeCheckingMode in VSCode's settings is set to 'strict'. See also: code.visualstudio.com/docs/python/settings-reference.
@davidkaftan5563
@davidkaftan5563 2 жыл бұрын
@@ArjanCodes awesome, thank you!
@sh0ot3r12
@sh0ot3r12 2 жыл бұрын
So, seems that protocols provides more elasticity for developers but on the other hand abstract classes make the things more ordered I would say. Do I think on a right way? I just started to discover the world of software development. I have a question, do abstract classes may contain implementation of some methods or its not recommended? Btw. Your videos are great source of knowledge! I really appreciate Your work! Thanks for this!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
That’s a good way of looking at it. ABCs establish a hierarchy (and thus provide structure), protocols establish a contract (and thus define the interface of what a certain piece of code needs).
@rezasadeghi7007
@rezasadeghi7007 14 күн бұрын
Content was so useful for me. but I have one question. As you mention the protocol is the natural way in python. Why we can not have interface for each of instance of each protocol. In your Device abstraction class some how we can say that you violate interface segregation in SOLID principle. Somehow in Protocol some of the dependencies are hide ?
@DMSBrian24
@DMSBrian24 Жыл бұрын
When a project is modular for users to expand functionality of and eg. add new devices, or when someone new is getting into an existing project, won't protocols make it much less obvious what methods have to be implemented to fit a given interface, or even where said interface is located? When you encounter the code defining different strategies, unless a comment describes what it does, it might be impossible to know that it's meant to plug into any interface, sure it reduces coupling but it also makes code kind of hard to follow and make it ambiguous how to implement new algorithms/devices etc.
How To Easily Do Asynchronous Programming With Asyncio In Python
23:09
Protocols vs ABCs in Python - When to Use Which One?
15:31
ArjanCodes
Рет қаралды 29 М.
Эта Мама Испортила Гендер-Пати 😂
00:40
Глеб Рандалайнен
Рет қаралды 10 МЛН
КАРМАНЧИК 2 СЕЗОН 4 СЕРИЯ
24:05
Inter Production
Рет қаралды 667 М.
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Рет қаралды 130 МЛН
Protocols in Python: Why You Need Them - presented by Rogier van der Geer
28:40
EuroPython Conference
Рет қаралды 10 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 350 М.
7 Python Code Smells: Olfactory Offenses To Avoid At All Costs
22:10
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 875 М.
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 92 М.
The Best Keyboard for Developers With a Major Flaw
7:29
ArjanCodes
Рет қаралды 2,6 М.