1 00:00:08,829 --> 00:00:10,005 Welcome back. 2 00:00:10,005 --> 00:00:14,530 So I'm going to start by defining a class to represent a person. 3 00:00:14,530 --> 00:00:15,880 So I'm going to say class Person. 4 00:00:15,880 --> 00:00:21,748 And let's say that every person has a name and year that they were born. 5 00:00:21,748 --> 00:00:26,510 So I'm going to create a constructor, def __ init. 6 00:00:26,510 --> 00:00:29,942 That accepts a name and a year_born and 7 00:00:29,942 --> 00:00:34,728 assigns instance variables for name and year_born. 8 00:00:34,728 --> 00:00:36,625 Self.name = name. 9 00:00:36,625 --> 00:00:41,720 Self.year_born = year_born. 10 00:00:41,720 --> 00:00:45,561 Now let's suppose that I also want to have a method getAge. 11 00:00:45,561 --> 00:00:49,783 So getAge that takes this year_born and 12 00:00:49,783 --> 00:00:54,384 let's suppose that we have a current year. 13 00:00:54,384 --> 00:00:59,554 So I'll define CURRENT_YEAR to be 2019. 14 00:00:59,554 --> 00:01:04,630 So getAge is going to subtract CURRENT_YEAR. 15 00:01:04,630 --> 00:01:11,223 So I'll say return CURRENT_YEAR- self.year_born. 16 00:01:13,180 --> 00:01:19,820 And let's suppose that we also want to override the __str__ method, 17 00:01:19,820 --> 00:01:24,977 so I'll say def __str__, to print out self.name. 18 00:01:27,997 --> 00:01:30,154 So, I'll use the dot format method. 19 00:01:30,154 --> 00:01:37,206 And .format self.name and 20 00:01:37,206 --> 00:01:41,323 self.getAge. 21 00:01:41,323 --> 00:01:45,006 Okay, so now we have a class to represent a person and 22 00:01:45,006 --> 00:01:48,792 every person has a name and a year that they were born. 23 00:01:48,792 --> 00:01:53,860 So if I create something like alice 24 00:01:53,860 --> 00:01:59,640 equals a new person named Alice Smith. 25 00:01:59,640 --> 00:02:02,882 And I print out the value of Alice, so 26 00:02:02,882 --> 00:02:08,017 after passing the year_born so I'll just say 1990. 27 00:02:11,611 --> 00:02:13,999 Oops, I need one more closing parenthesis here. 28 00:02:13,999 --> 00:02:19,779 Okay, so now I can see that Alice Smith is the name, 29 00:02:19,779 --> 00:02:26,107 and 29 is the age given that the year born is 1990. 30 00:02:26,107 --> 00:02:30,714 Of course, we're just going to ignore things like exact birthdays for 31 00:02:30,714 --> 00:02:31,720 this purpose. 32 00:02:31,720 --> 00:02:37,378 Now, suppose that I wanted to define a new class to represent a student, and what I 33 00:02:37,378 --> 00:02:43,046 want a student class to have is everything that a person has, plus one more thing. 34 00:02:43,046 --> 00:02:47,337 So I want students to have this notion of knowledge. 35 00:02:47,337 --> 00:02:52,978 So what I'm going to do, is I'm going to start by just copying and 36 00:02:52,978 --> 00:02:55,248 pasting my person class. 37 00:02:55,248 --> 00:03:01,201 So I can this, and then let's suppose that I want to have another instance variable, 38 00:03:01,201 --> 00:03:04,525 named knowledge, which I'll start out as 0. 39 00:03:04,525 --> 00:03:10,620 So I'll say, self.knowledge = 0. 40 00:03:10,620 --> 00:03:13,213 And I want this to be student, instead of person. 41 00:03:15,541 --> 00:03:19,896 And let's suppose that I want to have this other method on 42 00:03:19,896 --> 00:03:22,808 a student named study, so def study. 43 00:03:25,608 --> 00:03:32,811 And whenever a student studies, then their knowledge should go up, 44 00:03:32,811 --> 00:03:37,418 so I'll say self.knowledge +=1 okay. 45 00:03:37,418 --> 00:03:42,639 So, now if I define Alice to be a student instead of just a person. 46 00:03:42,639 --> 00:03:46,026 By saying, alice equals a new student. 47 00:03:48,257 --> 00:03:52,917 Then you can see that I still get Alice Smith, age 29. 48 00:03:52,917 --> 00:03:59,479 If I print out alice.knowledge, then I should get 0. 49 00:03:59,479 --> 00:04:05,247 If I tell alice to study, and then print out alice's 50 00:04:05,247 --> 00:04:10,238 knowledge level, then I'm going to get 1. 51 00:04:10,238 --> 00:04:14,718 Now, this is a fine way of defining both the person class and the stream class. 52 00:04:14,718 --> 00:04:19,077 But there is a much easier in more concise way of actually doing this using 53 00:04:19,077 --> 00:04:21,090 something called inheritance. 54 00:04:21,090 --> 00:04:25,945 The idea of inheritance is that you can define classes that inherit from other 55 00:04:25,945 --> 00:04:26,624 classes. 56 00:04:26,624 --> 00:04:30,176 So, in other words, they take all of the instance variables, 57 00:04:30,176 --> 00:04:32,923 all of the methods that that other class has, and 58 00:04:32,923 --> 00:04:36,356 then they can add more instance variables and more methods. 59 00:04:36,356 --> 00:04:41,294 So, in this case, every student is a person. 60 00:04:41,294 --> 00:04:44,672 It's not true that necessarily every person is a student, but 61 00:04:44,672 --> 00:04:46,147 every student is a person. 62 00:04:46,147 --> 00:04:50,946 So, what I'm going to do is I'm going to say that this student class should 63 00:04:50,946 --> 00:04:52,406 inherit from person. 64 00:04:52,406 --> 00:04:56,521 So a student should have everything a person has plus more. 65 00:04:56,521 --> 00:05:00,927 Now, the way that I say I want a student to inherit from person is by putting 66 00:05:00,927 --> 00:05:03,852 parenthesis after the class declaration here. 67 00:05:03,852 --> 00:05:08,182 And putting the class I want to inherit from inside of those parentheses. 68 00:05:08,182 --> 00:05:11,814 So I'll say that every student is a person. 69 00:05:15,167 --> 00:05:19,207 So now that I do this I can get rid of some of the repetitive parts 70 00:05:19,207 --> 00:05:20,700 of the student class. 71 00:05:20,700 --> 00:05:25,744 So here for example getAge, is a little bit repetitive. 72 00:05:25,744 --> 00:05:28,145 I don't need getAge in both student and 73 00:05:28,145 --> 00:05:31,363 person because getAge is already inside of person. 74 00:05:31,363 --> 00:05:35,753 So I'm going to remove getAge from the student class. 75 00:05:35,753 --> 00:05:39,550 I'm going to do the same for the def__ str__ method. 76 00:05:39,550 --> 00:05:41,897 And I'm going to do one more thing as well. 77 00:05:41,897 --> 00:05:47,179 So this setting self.name and self.year_born is a little bit repetitive 78 00:05:47,179 --> 00:05:51,811 with the Person class setting self.name and self.year_born. 79 00:05:51,811 --> 00:05:54,781 So in the constructor for the student, 80 00:05:54,781 --> 00:06:00,817 I'm going to say that I want to call the constructor, this method, of Person. 81 00:06:00,817 --> 00:06:04,536 And the way that I'll do that, 82 00:06:04,536 --> 00:06:09,546 is I'll say Person.__init__, and 83 00:06:09,546 --> 00:06:15,854 I'm going to pass in self, name, and year_born. 84 00:06:15,854 --> 00:06:20,707 Now, when I do this I'm going to call this init method, or 85 00:06:20,707 --> 00:06:24,687 the Person constructor, on this new student. 86 00:06:24,687 --> 00:06:27,286 So this is going to call this method. 87 00:06:27,286 --> 00:06:31,894 And then this method is going to set instance variables for 88 00:06:31,894 --> 00:06:35,194 name and for year_born automatically. 89 00:06:35,194 --> 00:06:38,906 So let's test this out with Alice, and be sure that our code still works. 90 00:06:41,878 --> 00:06:48,636 So you can see again we have our student Alice Smith born in 1990. 91 00:06:48,636 --> 00:06:53,520 And if I still run this code you can see that after Alice studies then Alice's 92 00:06:53,520 --> 00:06:54,672 knowledge is 1. 93 00:06:54,672 --> 00:07:01,445 But now if I print out alice.getAge. 94 00:07:01,445 --> 00:07:03,427 You'll see that Alice is 29. 95 00:07:03,427 --> 00:07:12,332 If I print out Alice, Then you'll see that we get Alice Smith, age 29. 96 00:07:12,332 --> 00:07:18,159 And the important part here is that even though Alice is a student, 97 00:07:18,159 --> 00:07:25,460 and even though the student class didn't directly define __str__ or getAge. 98 00:07:27,612 --> 00:07:31,018 I'm able to call both of these methods on alice, 99 00:07:31,018 --> 00:07:35,755 because alice is a Student, and Student inherits from Person, and 100 00:07:35,755 --> 00:07:40,343 the Person class has these methods, getAge, and __str__. 101 00:07:40,343 --> 00:07:42,058 That's all for now, until next time.