Rock Solid Python with Python Typing Transcripts
Chapter: Orthogonal Typing
Lecture: Structural Typing Visualized
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So we saw that we can dramatically reduce how much object hierarchy and object-oriented
0:07
programming we have to inflict onto our code just to get checking that some capabilities exist to kind of force a static version of that duck typing.
0:19
Well with structural inheritance and protocols, we can dramatically simplify our object hierarchy. Maybe keep it really shallow.
0:27
We can do a little bit more composition, a little less inheritance. Those types of good coding practices.
0:34
And we just create a class, give it whatever name we want, say it derives from typing.protocol, lay out all the capabilities that it's going to need
0:43
for us to use it in this way, and then the type system actually checks whether or not it's actually deriving from that thing.
0:51
It doesn't matter. It doesn't matter. It's going to just look at the capabilities or the attributes of a class or object being used and says,
0:59
does it match what we need by the type? Does it have a turn on, turn towards and accelerate? Yes, good to go. If it doesn't, not good to go.
1:06
Definitely look into seeing how this would fit into your programs because it's a really lightweight way,
1:13
kind of bring back some of the original joy of Python, that duck typing style, but with a little more safety.