
#365 Inheritance, but not Inheritance!
Python Bytes
Python Protocols and their Usage
The speakers discuss Python protocols and their usage, comparing them to class inheritance, mix-ins, and abstract base classes. They provide an example of a protocol and mention how it allows for type checking and verification even with third-party packages.
00:00
Transcript
Play full episode
Transcript
Episode notes
Speaker 2
Yeah, so Yeah, all right indeed Well There probably is some protocol around
Speaker 1
Elect the election results, right? No, you could model it with classes or you could functionally model it like in a immutable way But I don't know about a protocol. Okay
Speaker 2
We're just great at transitions on this podcast The next I want to talk about Python protocols So there's an article from Carlos Vassina I think Called the Python protocols defining a protocol and when to use it and actually This is something that I have I've been meaning to play with for actually some years And I haven't really done much with it yet. So I really appreciate this article. I was talking about Protocols are a feature of Python that were added in Python 3.8. So really anything maintainable right now We can use this can you can use protocols, but they're And you kind of they're Mentally I think of them kind of like like class inheritance or it mix-ins or abstract base classes and That's one of the things he talks about this in this article is is they are in that same similar space? But but you use them a little different and you might use them together with other with other forms too Like with with mix-ins and abstract base classes So the article just goes through on how to define a protocol and really and He's also discussed you also a decent tutorial on abstract base classes and mix-ins as well Which is nice to kind of describe them all together But the the protocol thing is just sort of let me see if I find an example So you inherit you have a class that's inherited from protocol and you give it You kind of give it function definitions, but don't fill in the body So that's kind of what a protocol is and then other Other classes that you that use the protocol derive from that you know from that protocol from like in his example there's a class called explainable that something that has an explained function and So you would derive from Explainable and then your new class would have would would be a instances of protocol But all it really says is that you can other places that use it for types and stuff can declare that they need some Need a protocol passed in or a explainable Class and then you can use anything that derives from that so pretty cool
Speaker 1
Yeah, right. Let me jump in to say one extra point here like what's awesome about this stuff is We've had duck typing like this There's an assess fairness function that it has to call explain on the object passed in and duck typing said Well, if it takes that if you can pass it in there and it will run when you call explain on it It must fit right but the typing tools don't check so like pie charm for example when it's go well It's or whatever so good luck with that But once you do this protocol stuff if you say the function takes an explainable and you have explained well as a protocol You can pass stuff in and it doesn't even have to derive from or be related to that Protocol in a base class like anything that is passed in there The type system will look at it and verify it hasn't explained even if it's in a third-party package and you don't control it So it's like a way to project typing structure onto a dynamic thing that is not necessarily your code. It's wild.
Speaker 2
Oh cool I kind of had that a little bit wrong then awesome. You
Speaker 1
can drive from it It gives you more information potentially, but it doesn't have to but you don't even have to which that's the totally wild aspect That's what I think is really different for this.
Speaker 2
Okay. Yeah. Oh Awesome That's pretty cool No, yeah, so clearly I haven't thoroughly read this article But I do want to get into really getting my head around abstract base classes mix ins and Protocols and stuff and
Speaker 1
probably say that though because like your experiences in C++ mine Also since you plus plus and C sharp and all those languages have these interface ideas, but there's their They're put into the type system through inheritance And so you can inherit from it and it does what you expect and it seems the right thing But what's weird is you don't even have to that's what's weird about it. Okay cool. Oh
Speaker 2
Awesome Now I definitely want to play with it more Yeah, cuz I've kind of missed that aspect of C++ in Python. Yeah So nice anyway, also I'm assuming it's a AI generated image, but really cool image at the top of the article
Speaker 1
I'm not sure about my opinion is about it. I'll ask chat GPT how I feel about it
Speaker 2
It's hard to describe how I feel so I'll you know Yeah, I'm actually sort of I know this is a tangent, but I've switched to I was playing with like bang is my primary search engine at work I just I don't know that a new computer It just was there and I'm just going with it for a while But everything I search is like gives me an AI generated answer first and I'm like I don't Don't know if I like that very much.
Topics covered in this episode:
- * Hatch v1.8*
- svcs: A Flexible Service Locator for Python
- Steering Council 2024 Term Election Results
- Python protocols. When to use them in your projects to abstract and decoupling
- Extras
- Joke