Python (Fall 2026) Module 14-4: Initializing a Class
Overview
Bill Moseley explains how Python's `__init__` method initializes new class instances, accepts constructor arguments, and stores data as instance properties using `self`. A car example shows how Toyota and Ford objects keep independent makes and mileages, illustrating why instance-specific data is useful in larger programs such as employee databases.
Key takeaways
- Python calls a class's `__init__` method automatically when creating an instance, making it the standard place to set up initial object state.
- Constructor arguments such as `strMake` must be passed during object creation when `__init__` requires them.
- Assigning a constructor argument to `self.strMake` stores the value on that particular object for use by its other methods.
- Instance properties keep data separate: the Toyota's 138 miles remain unchanged when the separate Ford object is driven 1,000 miles.
- Classes can model records such as employees, with each employee instance storing its own personal and contact details.
Chapters
- Initialization sets up an object when an instance is created, like preparing a clean whiteboard before a lecture.
- A method named `__init__` runs automatically when code creates an instance such as `obCar1 = clCar()`.
- Every method takes `self`; adding other parameters to `__init__` means the caller must supply matching arguments.
- A `strMake` constructor parameter can be stored as `self.strMake`, making it a property of that car instance.
- Creating a Toyota without supplying the required make argument causes an error; passing `Toyota` lets the constructor initialize it.
- Moving properties such as wheels, doors, miles, and make into `__init__` lets other methods access them through `self`.
- The example creates a Toyota and a Ford as separate `clCar` instances, then drives the Ford 1,000 miles.
- After driving the Toyota 100 miles and then 38 miles, `obCar1.intMiles` remains 138 even after the Ford is driven.
- `self.strMake` and `self.intMiles` refer to the properties of the specific instance, so changing one car does not change the other.
- The same pattern can represent employees with individual birth dates, Social Security numbers, addresses, and phone numbers.
Transcript excerpt
0:00 So, when we, when we create an instance of a class, when we create an object based on a class, one of the things that we say that it, the class does, is called initialization. All initialization means is that it's setting it up and getting it ready to go. Okay, that's the simplest definition of initialization. It's sort of like if I was giving an old-school lecture, I would wipe down the whiteboard before I started lecturing. It's like starting over with a clean slate, kind of getting it set up and ready to go. So, one thing that we can do is we can define what's called an initialization method. And an initialization method looks like this. It starts out with the
0:56 define. And it starts out with two underscores, i n i t, and two underscores. That's a special, specific name that says, "When you create an instance of this object, you will do this automatically." Now, if the initialization method, obviously it's going to require self, because every method requires self. Never forget that. But, if I put additional parameters here, then I have to give those parameters when I create an instance of that object. So, in this case, I have intCarWheels and strCarMake. And all it's doing is, it says, "When I create the object, I'm going to give it this, and I'm going to give it this. I'm going to give it the make of the car
1:50 and the, how many wheels it has." So, all it's going to do is it's going to take those things, and it's going to make properties. self.intWheels belongs to that car, self.strCarMake belongs to that. So, it's going to take whatever I put in here and it's going to put it in those. So, if I create a car called myCar, and, and I set it to clCar four and Ford,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.