1 00:00:00,690 --> 00:00:03,190 ANA BELL: The following content is provided under a Creative 2 00:00:03,190 --> 00:00:04,730 Commons license. 3 00:00:04,730 --> 00:00:07,030 Your support will help MIT OpenCourseWare 4 00:00:07,030 --> 00:00:11,390 continue to offer high-quality educational resources for free. 5 00:00:11,390 --> 00:00:13,990 To make a donation or view additional materials 6 00:00:13,990 --> 00:00:17,860 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,860 --> 00:00:18,830 at ocw.mit.edu. 8 00:00:23,760 --> 00:00:27,930 ANA BELL: So I have here-- first part is just the top part, 9 00:00:27,930 --> 00:00:29,910 so I have some blank line and then I'm 10 00:00:29,910 --> 00:00:34,030 defining a method name speak. 11 00:00:34,030 --> 00:00:35,650 So write a line to replace blank that 12 00:00:35,650 --> 00:00:40,030 creates a class for a Dog that inherits from Animal. 13 00:00:40,030 --> 00:00:41,740 So the first thing I notice is that I 14 00:00:41,740 --> 00:00:45,470 need to write a definition for a class whose name is Dog. 15 00:00:48,360 --> 00:00:51,010 So my choices are these. 16 00:00:51,010 --> 00:00:54,790 So I can tell it's either the first or the third one. 17 00:00:54,790 --> 00:00:56,950 And then I want to inherit from Animal, 18 00:00:56,950 --> 00:00:59,530 so I don't want to inherit from object 19 00:00:59,530 --> 00:01:01,420 but I want to inherit from Animal, right? 20 00:01:01,420 --> 00:01:04,599 So that's perfect. 21 00:01:04,599 --> 00:01:06,970 Next question says, with this definition of Dog, 22 00:01:06,970 --> 00:01:09,460 you run a program with those three lines. 23 00:01:09,460 --> 00:01:11,900 What happens? 24 00:01:11,900 --> 00:01:15,630 So this is my definition and I have these three lines. 25 00:01:15,630 --> 00:01:17,900 Let's go down here. 26 00:01:17,900 --> 00:01:18,600 Nice. 27 00:01:18,600 --> 00:01:19,100 OK. 28 00:01:22,010 --> 00:01:24,830 So with these three lines, this first line says, 29 00:01:24,830 --> 00:01:26,180 d is equal to Dog 7. 30 00:01:26,180 --> 00:01:31,980 So I'm creating a Dog object with age 7. 31 00:01:31,980 --> 00:01:34,470 Is this line going to throw an error? 32 00:01:34,470 --> 00:01:37,950 Well, this line actually looks for an init method. 33 00:01:37,950 --> 00:01:41,990 This particular class definition doesn't have a init method, 34 00:01:41,990 --> 00:01:44,930 but hey, I'm inheriting from someone. 35 00:01:44,930 --> 00:01:46,530 I'm inheriting from Animal. 36 00:01:46,530 --> 00:01:48,570 Does Animal have an init method? 37 00:01:48,570 --> 00:01:51,030 And it does, as we saw in the slides. 38 00:01:54,120 --> 00:01:59,890 OK, so we're creating a new Dog with age 7 and name none. 39 00:01:59,890 --> 00:02:01,390 So that line doesn't throw an error. 40 00:02:01,390 --> 00:02:03,720 The next line sets the name to Ruffles. 41 00:02:03,720 --> 00:02:05,340 And again, I don't have a set name 42 00:02:05,340 --> 00:02:08,190 method inside this particular class, 43 00:02:08,190 --> 00:02:10,919 but does my Animal class-- my parent class-- 44 00:02:10,919 --> 00:02:11,880 have a set name method? 45 00:02:11,880 --> 00:02:14,430 And yes it does-- right here. 46 00:02:14,430 --> 00:02:16,710 So I call that one. 47 00:02:16,710 --> 00:02:18,690 So that line does not throw an error. 48 00:02:18,690 --> 00:02:22,020 And the third line says d.speak, which 49 00:02:22,020 --> 00:02:24,450 is going to cause Python to look in this current class 50 00:02:24,450 --> 00:02:25,020 definition. 51 00:02:25,020 --> 00:02:27,420 And it says, hey, I have a method here named speak, 52 00:02:27,420 --> 00:02:29,340 so I'm going to use this one. 53 00:02:29,340 --> 00:02:31,800 And it's going to print ruff ruff, because it's a method, 54 00:02:31,800 --> 00:02:38,697 so you can just-- it's like a function. 55 00:02:38,697 --> 00:02:39,780 You can just print things. 56 00:02:39,780 --> 00:02:41,920 You don't need to return anything. 57 00:02:41,920 --> 00:02:45,243 So, perfect, most of you are getting that right.