Using Custom Protocols in Python
00:00 Now that you’ve created your first custom protocols, it’s time to use them. And in order to use a protocol, you need to know two things. The first thing is that classes that implement the protocol do so implicitly.
00:12 The protocols you define you’re not supposed to inherit from them. For example, when you define the whale to be a swimmer, you do not inherit from swimmer.
00:21 However, type hints use protocols explicitly. If you want a function that accepts a swimmer, for example, you need to type hint the parameter as a swimmer. That’s where protocols are used explicitly.
00:33 That’s what tells type checkers, that’s the variables inside the function or that the given parameter, for example, has access to certain methods. So let’s see how you can do that in your own code.
00:46 Go ahead and open the code from the previous lesson.
00:50
Go ahead and create a function called underwater
_exploration()
, which is a function that’s going to accept a single entity and that is going to make this entity explore underwater.
01:01 So for example, by telling it to dive to a depth of 50, and let’s not argue over units, and let’s make the entity swim up to a shipwreck.
01:14
Now the question is, what should the type hint for entity be? Well, if you do something like int
, this is obviously wrong and mypy or any other type checker will complain about this.
01:26
For example, if you open your terminal, I’m opening VS Code’s built-in terminal with a keyboard shortcut here. And if I run mypy on the fauna.py
file, mypy will complain.
01:40
And that’s because integers do not have the methods dive
nor swim
. However, if you replace the type hint with Swimmer
, mypy will no longer complain because swimmers do have the methods dive
and swim
.
01:57
So this is how you use protocols explicitly in type hints. Now how do you create a class Whale
that implements the protocol Swimmer
.
02:04 Well, go ahead and close your terminal.
02:07
Let’s define a class Whale
. Now, you could implement the hierarchy structure you saw in the diagrams from the slides from the previous lesson, but to really drive the point home that protocols are orthogonal to the class hierarchy, you can also just implement the class Whale
without inheriting from Mammal
, without inheriting from Animal
, without nothing, just class Whale
.
02:31
And to implement the protocol, again, you do not inherit from the protocol itself. You just define the methods with the appropriate signature. So a dive()
of float
that does nothing or returns nothing.
02:43
And let’s just say print(f"Whale is diving to {depth = ].")
just so we can see something on the screen. And also you need to implement the method swim()
that accepts the string destination
02:58 also returns nothing, and prints that the whale is swimming to a given destination.
03:09
And now you can create an instance of a Whale
,
03:14
and you can call the function underwater_exploration()
03:19 with your instance. And not only should the code run, but mypy should also not complain because the whale is defining the methods it needs. So go ahead and open the terminal
03:31
and if you try to run mypy on your file, you will see that mypy does not complain. Why? Because you passed in a whale to the function underwater_exploration()
.
03:43
And the function underwater_exploration()
, if you look at its code, the function underwater_exploration()
expects an entity that must be a Swimmer
.
03:53
The Swimmer
, in turn, is a protocol that defines two methods with these signatures you can see on the screen. dive()
should accept a float
for the depth, and swim()
should accept a string
for the destination.
04:07
And your class Whale
implements these methods exactly. I’m going to go ahead and open the same file on a second pane and close the terminal so we can look at both things at the same time.
04:20
So on the left we are looking at the protocol Swimmer
, and on the right we are looking at the Whale
implementation. And you can see that they define the same methods: dive()
both in the Swimmer
and on the Whale
, and swim()
both in the protocol and the Whale
.
04:38 And that’s how static type checkers can tell that you implement the protocol. You do not have to inherit from it. So this is implicit, but the static type checker is able to catch that.
04:50 So this is how you can use protocols. Now in the next lesson, you are going to learn how to build generic protocols so that you can create protocols that are even more flexible.
Become a Member to join the conversation.