Intermediate Python Initializer Method
Transcript from the "Initializer Method" Lesson
>> Nina Zakharenko: These classes, they take a, if we want to be able to instantiate them and pass in arguments to them, like we did with int, we need to make the special type of method. That's called the initializer method and it starts with these double underscores or __init__. What do these double underscores tend to mean?
>> Speaker 2: Special.
>> Nina Zakharenko: Yeah, special. So you shouldn't be calling this initializer method directly, it kind of happens under the hood.
>> Nina Zakharenko: I'm gonna print out does run and I'm gonna access runs, like runs is a class variable, right? So if I run this code, well, the syntax highlighter here is giving you a little bit of a hint that I forgot something.
>> Nina Zakharenko: Let's say I didn't have this at all, and I'm just gonna print out new car.
>> Nina Zakharenko: Syntax highlighter is still giving us a little bit of a hint there. Why is that? It's because I'm missing something, and.
>> Speaker 3: Self.
>> Nina Zakharenko: Right.
>> Nina Zakharenko: So self is a required argument to __init__.
[00:01:35] It's kind of a hidden one, you don't have to explicitly pass it in. I'm going to import some things, so that I can pull in the newest version of my car class as I modify it. So if you're following along with me in this by using a file, you'll have to do this as well.
[00:01:53] So you would have to import importlib and then say importlib.reload and the name of your package, in this case, it's cars.
>> Nina Zakharenko: If I can just type today. The reload must be a module. Okay, so, to do this kind of live reload in the [INAUDIBLE], not something that we usually do.
[00:02:23] Usually we're either working just in the [INAUDIBLE] or just with files. I would need to import cars as a module, and then, I can say importlib.reload and give the package name of cars. And that'll just reload it from my session. Make sense? So now if I wanna make a new car, I just have to say, cars.car, and to verify that the type of that is a class type.
[00:02:56] Okay, so if I try to make a new car now.
>> Nina Zakharenko: I'm not gonna be able to access any sort of instance variables on it. And we'll see here that the linter is giving me a hint on that. So in order to do that, I'm gonna have to pass it in self.
>> Nina Zakharenko: And let's change things around a little bit. I'm just gonna close this. I don't want to specify a new name every time I start my car. It doesn't make sense, right? When I make my new car instance, I should be able to say what the type of it is, or I'm sorry, what the model of my car is once, not every time I start it.
[00:03:54] It should just know. So I'm gonna move this name variable.
>> Nina Zakharenko: I'm gonna move that into the init.
>> Nina Zakharenko: What do I need to do to save it to the instance?
>> Speaker 3: Self.name.
>> Nina Zakharenko: Self.name=name. Now, it can take this out. Am I passing a name into the start method here?
[00:04:22] I'm not, I still have to pass in self to signify that this is an instance method. Now, I can still access self.name.
>> Nina Zakharenko: But I don't have access to it in this particular method, and it's all via the magic of self. So let me reload this.
>> Nina Zakharenko: And now I can say my_subaru = cars.Car.
[00:04:54] And I'm gonna give it a name of Subaru.
>> Nina Zakharenko: And cool, new car. Now I can say my_subaru.runs.
>> Nina Zakharenko: Starts. I don't know why I got runs into my head. Okay, my_subaru.starts.
>> Speaker 3: Start.
>> Nina Zakharenko: Start. Thank you guys, my_subaru.start. And start knows that the name of my car is Subaru, via that instance variable.
[00:05:26] Okay, that's kind of the basics of classes here. That self refers to an instance.
>> Speaker 4: So the method start-
>> Nina Zakharenko: Oops, yes.
>> Speaker 4: The way it reads self.name is Linda's program initializes, that's where self.name. I'm just wondering how it reached ten.
>> Nina Zakharenko: So let's say I had a new car my_Mustang=cars.Car.
[00:05:59] What do we know about positional arguments?
>> Nina Zakharenko: They are required, right, and they have to be passed in in order. So now if I try to make a new car, and I don't pass in that name argument, what's gonna happen? I'm gonna get a tape error. It's gonna tell me that init is missing one required positional argument of name.
>> Nina Zakharenko: So I know that if I have an instance of a car because I'm asking for it in the constructor, and I'm setting self.name as name. I know that it's guaranteed to be there as a variable.
>> Nina Zakharenko: To show you one more time, it looks like I didn't correctly reload my module.
[00:06:48] So let me show you, if I make an init and that does not take self, and let me correctly reload this time. if I say cars.Car, if I try to make a new instance now, I'm gonna get a TypeError. It says init takes 0 positional arguments but 1 was given.
[00:07:13] Weird, right? What position or argument was given? We didn't pass any in.
>> Speaker 4: Self.
>> Nina Zakharenko: Self, exactly right, but that binding happens under the hood, right? So when you make a new instance of a class, Python will always pass in self as the first argument.
>> Nina Zakharenko: Okay.
>> Nina Zakharenko: And so let me make sure to pass in that name argument again.