Methods

Flash and JavaScript are required for this feature.

Download the video from iTunes U or the Internet Archive.

Description: This in-class exercise demonstrates class method construction 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: So we're given this definition for car. We saw that on the previous slide. I want to add a method that's going to change the color of the car, and these are my four choices. And it looks like you guys are getting it right, which is awesome.

So to find a method to change the color of the car, which does this. So we know that self has to be the first parameter, so we can automatically eliminate A and C and it's between B and D. And remember I said you have to be conscious about whose data attribute you're accessing, and in this case, we want to change the color of a particular instance of the car, right? So we have to say self.color instead of just color. If we just said color, then color would refer to just a variable inside the class, not a data attribute of a particular object.

Free Downloads

Video


Caption

  • English-US (SRT)