Method Call

Flash and JavaScript are required for this feature.

Download the video from iTunes U or the Internet Archive.

Description: This question explains the how a class method changes a attribute is altered in Python.

Instructor: Dr. Ana Bell

The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To make a donation, or view additional materials from hundreds of MIT courses, visit MIT OpenCourseWare at ocw.mit.edu.

ANA BELL: This is my definition, including the method to paint the car, so to change its color. And the question says you create a car with this line here. So you're initializing it with 4 comma 2. What's a line of code that changes the color from whatever it initially is to red?

The first one, car.paintred, is sort of attempting to do the way that we saw in the right in the slides where you calling the class name dot. But it's missing the self. So we don't know what object to call it on. So that one's out of the running. The second one is closer, but we have red here as a variable as opposed to a string. So that one's not going to work. The third one looks good.

And the fourth one is just weird. I don't think that one will work. It's attempting to put a value for self. But you've already called mycar dot instead of the class name dot. So here, this isn't going to work. So since you already called the object name dot method name, you just have to give it the other parameter that it needs, which is the color. I think the majority have gotten it right.

Free Downloads

Video


Caption

  • English-US (SRT)