Declaration and Initialization

00:00 Welcome to lesson two in Object-Oriented Programming in Python versus Java. In your last lesson, we looked at a simple class in both Java and Python. In this lesson, we’ll start looking at more specific details, starting with declaration and initialization.

00:17 In Java, fields are typically declared at the top of the class then initialized in a constructor.

00:26 Here we have our class Car and we have defined three fields—the color, the model, and the year of the car. There’s our declaration. Those fields are then initialized in the constructor.

00:46 The constructor takes three parameters—car, model, and yearand since we’ve used the same names as the names of our fields, we have to use the word this when referring to the field to prevent the ambiguity. So we say this.color = color, this.model = model, this.year = year.

01:09 In Python, the declaration and initialization happen simultaneously in an .__init__() method. An .__init__() method is similar to Java’s constructor. As a programmer, what you would put in a Java constructor goes in a Python class’s .__init__() method.

01:30 So here is our .__init__() method with four parameters. We see the parameter names for color, model, and year, but we also have this first parameter, self. In Python, any method you write must have at least one parameter, and that parameter represents the name you’re going to use to refer to the calling object.

01:53 Python programmers have adopted the use of the word self to represent the calling object. This is similar to Java’s this keyword, only its use is required anywhere in a method that you want to refer to the calling object.

02:11 When I say self.color = color, I am defining a field, an attribute, for this class called .color, and I am assigning it to the value passed from the parameter for color. self.model = model, self.year = year. Again, similar to a Java constructor except with the additional first parameter, the word self, to refer to the calling object.

02:46 We can access the attributes directly using the dot (.) operator. So, let’s suppose that I want to use this Python class. I will import car and I will create a Car object.

03:03 Say, my_car = car.Car("silver") I have a 2018 Ford Fusion. Notice that slightly different syntax. Before you use the class name, when calling the constructor, you have to use the file or the module name containing that class. So my_car = car.Car("silver", "fusion", 2018).

03:32 And then I could do a print statement such as print(f"My car is"), and then I can say {my_car.color}, for example, and it will tell me My car is silver. And we could access the other attributes in a similar way.

03:54 You might notice that we haven’t said anything about public or private. You’ll learn later in this course that everything in Python is public.

04:03 As a Java programmer, you’ll wonder about information hiding and things like that. Python has developed a different mechanism to kind of create that hiding, which we’ll see later in this course.

04:16 Another interesting feature about Python is you can add attributes to an object already created.

04:25 So, for example, suppose that I want to indicate that my car has 4 wheels. Well, I can say my_car. and then I can give it a new field. I can say my_car.wheels, and my car has 4 wheels.

04:45 I have now created a new field for this particular instance of Car. And I can print out the number of wheels that my_car has. It has 4 wheels.

05:01 This new field only applies to this particular object. If I create a new object… So, let me create an object for my wife’s pickup truck. She has a red Chevy S-10 from 2000.

05:21 But then if I want to print out its value of the attribute .wheels,

05:32 we’ll get an error message telling us that the 'Car' object has no attribute 'wheels'. That was a special feature that we added to that first my_car object. In your next lesson, we will continue looking at attributes, specifically class attributes in Python.

Become a Member to join the conversation.