Creating Subprotocols
00:00 Hopefully by now, you know that to implement a protocol, you do it implicitly, you do not inherit from the protocol, But that’s only to implement it. Remember that protocols are still regular Python classes.
00:13 So whatever you know about Python classes applies to protocols, and inheritance can be used to create a hierarchy of different classes. Therefore, if you inherit from a protocol, you can create a hierarchy of protocols.
00:28
Or, in other words, if you want or need to create a hierarchy of protocols, you can do so through inheritance. Let’s show you an example of this. Go ahead and create a new file called content_creator.py
.
00:50
creator.py
. Once you have your new file open, go ahead and import Protocol
from the module typing
. Because you will be creating a base protocol.
01:03
Let’s say, for example, that you have a ContentCreator
protocol that specifies that classes that implement this must have a method called create_content()
.
01:16 Now you want to distinguish two types of content creators. So you first create a blogger content creator
01:27 that has a list of posts which are strings,
01:34
and a method called add_post()
, which takes, for example, a post title and the actual post contents and returns nothing. So this creates a Blogger
protocol that inherits from ContentCreator
.
01:48
And additionally, you want to create a Vlogger
(ContentCreator, Protocol)
.
01:57
And this one will have a list of videos, which are also strings, and a method called add_video()
that accepts, say, a string title for the video and maybe the path to the video on your computer.
02:15
Now looking at your three protocols, you can see that both Blogger
and Vlogger
inherit from ContentCreator
and from Protocol
.
02:26
Now, strictly speaking, you could delete the explicit inheritance from Protocol
. So if you delete that, you can have Blogger
and Vlogger
inherit only from ContentCreator
because they will also be inheriting from Protocol
.
02:41
But it’s considered the best practice to keep the explicit inheritance from Protocol
just for the sake of making it clearer that you are defining protocols.
02:50
If these definitions were spread across different files, for example, it would be easy to miss the fact that ContentCreator
is already a protocol, and therefore Blogger
and Vlogger
are also protocols.
03:02
So you have these explicit inheritances. And now the fact that both inherit from ContentCreator
means that to define a class that implements the protocol Blogger
, you will need to have a list of posts, a method add_post()
, but also the method create_content()
.
03:20
And similarly for Vlogger
. So as an example of this, what you can do now is try to implement a class that implements the Vlogger
protocol, for example.
03:31
So let’s say that this is a Vlog
. Now this must have a list of videos. So let’s say you initialize your list of videos as the empty list in the beginning, you must also have a method add_video()
as defined by the protocol Vlogger
itself.
03:50 And this should accept a title and the path.
03:55
And for now, let’s do nothing here. And because the Vlogger
is also a ContentCreator
, you also need a method create_content()
.
04:05 Let me go ahead and actually open the same file on the right so that we can take a look at the top of the file. So this is the same file on the left hand on the right, but in different locations.
04:18
And at the top on the right, we can see the original ContentCreator(Protocol)
. So now the Vlog
class must also implement the method, create_content()
.
04:26
I’m glad I opened it because I was about to create a method with the wrong name. So create_content()
takes no arguments and returns a string.
04:34 And let’s just say it returns the empty string for now.
04:38
Now what you can do is you can create an instance of Vlog
.
04:44
You can type check it as a Vlogger
, and you can ask your static type checker if this is fine or not. So go ahead, close the right pane, open the terminal,
04:56
and run your static type checker on these files. So content_creator.py
. And you can see that your static type checker is absolutely fine with this because your Vlog
class implements all the methods that it needs.
05:10
If you were to, for example, remove the create_content()
which was inherited from ContentCreator
, now your static type checker will complain
05:23
because you’re missing one method. Just to make sure that we end on a high note, you can go back and restore the method you just deleted. Make sure you can make mypy happy again. Restore create_content()
, and then run mypy to make sure that you have no errors in your file.
05:47 That’s it for inheritance with protocols. That’s it for creating subprotocols. Well done. You are almost at the finish line. You’re just missing a quick recap of everything you’ve been through.
Become a Member to join the conversation.