Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: Typed Python Motorcycles
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
It's good that we saw that Swift one last because it's going to inspire us to create
0:06
our Python, modern Python, strongly typed, somewhat strongly typed because it still run even if you get it wrong, right?
0:14
That's the gradual progressive stuff. We're going to create a typed version of the Python application here.
0:22
And what we're going to do is kind of gloss over, just give you a quick high-level view of what stuff looks like.
0:27
And then the next chapter is all about diving into the details and the nuances and all the language features.
0:33
So we're going to skim on this one just to give you a sense of like, well, how does this come out? How does this change in the Python world?
0:41
So there are no types, we have types, run it. Same output should always be getting the same output.
0:50
Alright, for this one, we're going to edit the types right now there are no types. And like I said, Swift took a lot of inspiration from Python.
1:02
So we have the colon type, as opposed to the traditional C wave where you have type, then
1:08
the definition, right, this will be a string model, or is this is a model, which is a string,
1:14
and it gives you the more important thing, what is it before it tells you details about it.
1:18
So over here in Python, we're going to say that this is a string, the type name is different. But other than that, it's the same as Swift, right?
1:28
It's not capital S string, just str. Style, just leave that alone for a minute. Engine size, this is an int, so we say colon int.
1:38
And this is a boolean, so we'll say colon bool. And interestingly, like Swift, this one cannot be set to none.
1:47
Bool, as opposed to bool question mark, which is not a Python thing, but the same idea.
1:52
In Python types, this can't be none, it has to be true or false. That's it. We saw all the other ones. Let's go grab from this one here.
2:04
We saw all the other ones had this cool enum that gave us more structure.
2:08
And I'm not sure that made sense when we had no types, but now it certainly does.
2:13
So in Python, we can say there's a class of this that derives from enum. Let's import, no, won't let us, but there you go. Now it imports it.
2:26
.enum of that type. And what are we going to have? We're going to have a sport. There we go. Fill them all out.
2:37
The only one we're really using is adventure. And we can do a little better, say, not this is just an enum, like a number, but this is
2:43
a string enumeration. So all the values in here are strings. And once we have this type up there, we can now say that our style has to be one of these.
2:53
So when we say self.style, you can see our list of adventure, trail, naked sport, motocross, etc. And there's no other options. That's it. Pretty cool.
3:05
We also do this to the parameters. So this is a string. This is a motorcycle type. This is an int. And that's a bool.
3:16
Okay, switching back over to the Swift one here. This is kind of the information we got there. And this is what the new one looks like.
3:25
And a little more verbose, but still quite good. It does tell us when we say things like engine size that these are integers, right?
3:34
See all the integer information coming over there. Okay, so that's the constructor.
3:39
Now our can jump, well, it's returning a bool, but this is already a bool.
3:45
So it turns out this call there, while maybe interesting and useful, is not really required.
3:50
But we somehow want to know when you go and work with the canJump property. So we could say canJump down here, what actual value is that here?
4:02
If we go to this, we can say, as we saw in Swift, this is a bool, canJump, true or false. And back down here, I guess bools.
4:12
It's showing us the bools are really in Python. Okay, but expressing that the return value, which tells you what type the property is, is cool.
4:21
This, we could go ahead and say returns a string. No one really calls it explicitly.
4:27
Just let the runtime call it, but let's make that real clear as well. This is going to be a model, which is a string and an int.
4:37
And here, and notice this is, there's a problem here. You just typed the word adventure, didn't you? That's not choosing from the list.
4:45
Where's the refactoring support and those kinds of things? Where's the auto complete? Doesn't tell me what I can do. It tells me what strings are.
4:52
We can say motorcycle type dot adventure. That's what was expected there. Perfect. And then down here, these are all the same.
5:03
Looks like it's pretty much ready to go. Let's run it. get exactly the same output and here's our are true from this can jump there. I'll take
5:11
that away for now. Anything we're missing, we could be a little more explicit on the
5:15
create bikes. This is going to return a list of motorcycle. So we'll say list bracket motorcycle.
5:22
Right when you have a collection that then contains things you say the collection or
5:28
container bracket the type that goes in there. As opposed to I said type. You see now there's
5:34
an error down here, it says, No, no, no, we are getting motorcycles in our list, not motorcycle types.
5:42
There we go, that goes back, we can even be really explicit here that this is a list of
5:49
motorcycle, although often the type inference from from the return value to there, even without this, you'll see. So it knows it's a list.
6:02
And if we grab an element, it knows it's a motorcycle. But sometimes it's helpful to go ahead and just be explicit there, which we can do like that.
6:10
Finally, if for some reason, like B didn't know that it was a motorcycle, PyCharm is pretty smart here.
6:15
And it does, but you could pre declare, you could say B colon motorcycle, because it's a thing coming out of a list of motorcycles.
6:22
So one of them is a motorcycle, you could pre declare it like that, see the still runs
6:28
like this. And now for sure when I say B dot, it's going to be a motorcycle. Maybe this
6:33
thing that came up here was not clear. So if we said this is a list of any, for example,
6:41
out of typing, we'll talk about that later. I go down here and type B dot, it's like I
6:46
have no idea what this is. Are you crazy? But if we pre declare it above outside the
6:52
list, now it's a motorcycle. Okay, so that's when you would use it. But we already have
6:56
enough type information elsewhere that this is not really needed. Here we have it. This is our typed version of our Python code.
7:10
It's pretty comparable to the Swift. To me it still feels even a little bit cleaner and it's certainly cleaner than the C# code
7:17
which is got, you know, this is not that different. But it's somewhat different. It's somewhat more verbose over here with symbols. So there it is.
7:28
This is the standard typing stuff that we're doing in Python.
7:33
We're going to dig way into a lot of the details here when you use them and so on in the next