What’s the Output? – Programming Foundations with Python

Okay, so here is the code for class Parent and now I will start to define class Child. Now, I know class Child inherits from class Parent and the way to indicate that in Python is to say this. The syntax here means that class Child will now inherit or reuse everything that is publicly available in class Parent. Now, things will get really interesting when we begin to define the init method, or the constructor for this class. So, we know that class Child has three variables. Last name and eye color that are inherited from class Parent. And this other variable called number_of_toys. So, in addition to the key word self, I will receive those values as arguments right here in the init function; there they are. Now, to initialize the variables I’m inheriting from class Parent, variables like last_name and eye_color, I will actually reuse class Parent’s init method. This is how we do it. The last instance variable, number_of_toys, will be initialized next. Okay, finally, I will add a print statement to the very beginning of the init method. All right, now that we have defined our class Child, the next thing to do is to create an instance of this class Child. I will call it, appropriately enough, miley_cyrus. And here, I will provide it the three necessary arguments. And then, I will print out two things. Her last name, and the number of toys. Also, so I can focus on these print statements in my output, I will comment out the previous instance for now. There. Let me save this file. Now, before I run this program, I want you to think about what the output of this program will be. So, here is the code one more time. Here is class Parent, and here is class Child, and here, we are creating an instance of class Child, and we named it miley_cyrus. So, what do you think will be printed when I run this piece of code? Enter your responses in this box.

Tags: , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *