Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Classes and Typing

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, let's talk about classes and Python types. I want to go back to our motorcycle example we did from chapter two, because most of what
0:09 we need to talk about is already there, and I don't want to overdo it in terms of time and starting over.
0:15 So, we've got our class motorcycle, and we've already played with classes like our person just previously.
0:20 And we've got our initializer, which plays a really important role because this is often where the fields of a class are defined.
0:28 So these are the four fields now, off-road, engine size, style, and model that are part of the motorcycle. So we can say the type of it just like this.
0:40 Self.offroad colon bool equals off-road. And we can also say at the call site what type it is. Off-road is a bool, right?
0:49 So when we create a motorcycle somewhere down here, we could say this is true, remember? and it's gonna say, no, no, no,
0:57 the thing you have to pass is a Boolean, you gave us some weird string thing. We talked about that previously. Right, so that's pretty standard.
1:05 When you have methods, even properties, you just like we do with our function conversation, we return, we say the type that returns, that's all good.
1:14 Here we've got a list of motorcycles, it's just a regular function actually. This is the end of the class.
1:20 But you might have noticed a couple of things. Sometimes our motorcycle, sometimes classes, can have class wide fields that can be overridden
1:30 on an instance, but typically are not. So for example, over here, I could say number of wheels. Right, so we could have wheel count equals two.
1:41 Do all motorcycles have two wheels? Well, I suppose that depends on your definition. We could be freaks and have something like this
1:48 with two wheels on the front. I don't know, like it's one interpretation of a motorcycle, but let's in our example say the motorcycle,
1:57 all motorcycles have a wheel count and the wheel count is set to be two, unless I guess you could override that in here.
2:05 But this is a class level field where these are instance variables, right, fields. So let's go over here and when we do the same thing,
2:13 we could just say that that is an int and down here at the bottom, we could print B dot. Now we have wheel count and if we run this,
2:20 you can see that two there on the end is the one that's coming from there, right? So if you wanna have a class level field, same thing kind of as here,
2:30 but just put the type up there. The more interesting thing is you might, maybe you caught this, maybe you didn't.
2:36 I didn't put a return type on createAdventure now, did I? So for that part, what does it return? It returns a motorcycle.
2:46 Obviously, Michael, it says right there, new motorcycle with these values. Cool, well, let's just say that. Let's say it returns a motorcycle.
2:57 Wait a minute, PyCharm says that's an error. That must be something stupid of PyCharm. We'll just ignore that. Nope, it really, really is a problem.
3:06 The new error messages in Python 3.11 are nice. Motorcycle, did you mean motorcycle type? No, I meant motorcycle. What is going on here?
3:15 we've got to go back to how Python works. There's no compiler in the traditional sense of Python.
3:21 There are PYC files and the stuff kind of gets compiled, but it doesn't do a pass through to look at all the types.
3:27 And then another pass through to say, now that I know where these are located, let me actually click these pieces together.
3:34 So for example, in this class here, we're defining motorcycle and Python just runs line 16 to line 37. And as the lines run, that defines the things.
3:44 is okay, we're gonna start defining a class. That thing has a wheel count, it has a function called init. Okay, great, it has a property called this.
3:52 And literally going step by step is defining it. And it's not till line 37 that it even knows what the class motorcycle is.
4:01 But how do I address the thing itself when I'm on line 34, but Python, the way it executes, doesn't actually have a thing called motorcycle
4:12 until the whole block of code runs. It's like a weird catch-22. So early days of typing, what you used to be able to do is you would say this.
4:21 It looks like an error, but it's not quite the right type. It says, okay, that's a class motorcycle. No, it's not, it's a string.
4:28 But if I go to motorcycle.createAdventure, I hover over it. Well, once I get the warnings away, hover over it. It says it returns.
4:41 you can see not in quotes, but an actual motorcycle. If I say dot, I get motorcycle auto-complete. So that was a way to kind of fix this
4:49 so that you don't really, 'cause you couldn't say motorcycle, right? The thing wasn't defined, so you put it in strings.
4:56 But what if there's two motorcycles somewhere in your program, right? It could get weird. So to be more concrete about this,
5:05 they added something called typing.self, right? we're familiar with self here, even though this is a class method, it's still use the typing.self.
5:17 And what you say is, I don't know what, I can't, because of this limitation, I can't say what class I'm in by name,
5:24 like a regular programming language like this. And I don't wanna use that string thing. I just wanna say whatever my name is,
5:31 'cause what if you change the name of the class, then it's no longer accurate, right? So I don't care what it's called.
5:38 I just want it to be, I return one of me. And the way you say I return one of me in the objects is typing.self. So we're already used to having self
5:49 to refer to the instance of an object. And so regardless of its class method or an instance method, static method, we say typing.self inside a class,
5:59 that's what gives you back the thing. So again, if we say motorcycle.createAdventure, We hover without warnings, we hover over it,
6:09 it says, now it returns a self, but it's coming out of the class motorcycle, and you hit dot, you can see, it's got all the motorcycle goodness here
6:19 because it knows it's the self parameter or the self type coming out of a motorcycle class, so that's the motorcycle class. Make sense?


Talk Python's Mastodon Michael Kennedy's Mastodon