What are "Protocols" In Python? (Tutorial 2023)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
recently I learned that python has something called protocols and Protocols are pretty cool this video is going to be centered on showing you basically how they work to get started we're going to import from typing the protocol type and again we are in Python so all types that we import or that we decide to use are not going to enforce anything they're just there for us as a developer to see if we're inserting a wrong type or not and without a code editor this might even be considered useless so if you're using a code editor such as pycharm or Visual Studio code this can bring a lot of benefits to your code and before we create anything I'll get a bit into the theory of protocols if you've ever programmed in a language such as Java or literally I guess any other language most programming languages have something called interfaces and with interfaces you describe how a class should be so anything that inherits from it should kind of Follow That kind of class so for example if you have a class called fruits and the fruit has a function that's called eats I don't know why but the fruit will eat itself and we'll just pass here if you have a class like this and it has this function and someday you want to inherit from this and you say class banana and you wanted to follow the guidelines of fruits usually you would use an interface for this so what an interface would do is ensure that we use that method inside our banana function because we are following the fruit interface and if I remember correctly in Java your code won't even compile if you do not follow the interface if you've defined one and I feel like I explained that really poorly so I might as well just create an example using the protocol so you have a better grasp on what it does so in this example we're going to create something called printable and this is a protocol so we're going to put that in there and now what we're going to do inside here is create that interface we're going to create that outline for everything that should conform to the printable protocol so for example everything that's printable will have pages and here we'll just say it's of type integer and this will be a class variable and then we will also have some methods that should be required such as print because you want to be able to print that printable whatever it could be a newspaper it could be a book and we'll just insert pass here and we also want to be able to recycle this stuff so we will also have something called recycle and we want that to be part of this protocol so what did we do here nothing yet we just created the guideline of what all the classes that conform to this protocol should look like so what we're going to do next is say okay here's a class called book and if we wanted to conform to the printable protocol we're going to have to make sure we have all of this inside book we can even have more than that but this is the minimum that we need to have inside book so let's do just that we'll say that it has some pages of type integer whatever we need to make sure we have that then we can also add some extra functionality such as an initializer and we'll say that this book has a title so he will type in title type string and self.title is equal to title and we will Implement that print method So Def prints and all we're going to do is print inside print and actually I don't really like this practice of naming it functions that already exist in Python so we'll just say print item and we'll do the same thing down here print item just to make sure we never Shadow this I know we're inside a class so that's not going to happen unless we use self so we're going to scrap the print idea and we're going to call it print item but here we'll type in printing book and we'll type in self dot title so we're going to print that book with that title the only method we're missing now is recycle So Def recycle and we can print recycling I will add these self dot title again and we will turn that into an F string so we have a book that conforms to this protocol but I still didn't show you where this becomes useful because I mean we still didn't do anything with the protocol we just copied essentially what we did in printable well down here we can go and create a function and now this is where it becomes interesting so we'll create this function that says printable as funky as that may sound and that's going to take a printable of type printable so now we have a parameter that takes this protocol as a type and inside here all we're going to do is get that printable and say that we're going to print that item so that's all that print printable is going to do now let's create an instance of our book we're going to say book of type printable is going to equal a book that says python so far so good we're not getting any complaints from our code editor all the types are working quite nicely and right below that we can print this printable which will be the book and if we actually print all of this we will get the output of printing book so that was pretty cool everything's working as it should although what was the point of making printable when you could have just said of type book well let's suppose we have different classes and we want to be able to print printables so in this example I'm going to insert a class called magazine and it's just going to take a title I'm not going to implement any of the methods that are required by the protocol of printable we're just going to put this initializer and that's all it's going to do so right below book we're going to create a magazine of type printable and that's going to equal a magazine and he will say okay we're going to call this python nistas that's going to be the name of the magazine immediately we're going to get a complaint from our code editor that we put a magazine inside there instead of a printable so that's already a major difference between book and magazine the printable protocol worked for a book as you could see we have print item recycle and pages and book has Pages print item and recycle and you can insert whatever else you want but that's the minimum a class must have to be considered printable magazine has none of that but we can easily fix that by saying pages of type integer and we'll just print all of this we'll just copy that paste it under and say printing magazine so we implemented everything that's required by the printable protocol to be considered a printable so now if we go down to magazine you'll see magazine of type printable is a magazine of pythonistas and we'll get no complaint there we're conforming to that protocol and just like earlier we can print this printable and we can insert the magazine and this time it'll say printing magazine pythonistas and as I mentioned earlier you can add as many methods as you want on top of this you can say Dev hello I don't know why but the magazine will say hello sometimes and it will print self dot title says hello and if we go down we will get no complaint from the code editor because we conformed to the protocol the printable protocol but at any moment if we remove something that's required by that protocol it's going to complain so that's something useful you can use with classes when you are type hinting and again a lot of languages have this and in statically type languages it is enforced but since python is not statically checked we're not going to get any errors for running the wrong type but we still can get those type hints if you are in a code editor so that was my simplified explanation of protocols I know it gets a lot more complicated once you start talking about ABCs and so on but that's actually all I wanted to cover in this video do let me know if I missed something or if you have some interesting information that you want to add on top of this I would love to hear about that in the comment section down below but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 26,064
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 2jN11lyKvfA
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Thu Jun 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.