1 00:00:00,000 --> 00:00:03,630 Hello, and welcome to Object Oriented Programming with your 2 00:00:03,630 --> 00:00:05,300 host, Kalob Taulien 3 00:00:05,300 --> 00:00:09,000 In this lesson, we're going to be talking about class methods. 4 00:00:09,000 --> 00:00:13,500 Now, a method is honestly, it's just a function inside of a class. 5 00:00:13,500 --> 00:00:14,700 That's literally all it is. 6 00:00:14,770 --> 00:00:19,010 But because it comes with a few extra goodies, like being 7 00:00:19,020 --> 00:00:23,060 able to access the 'self' keyword, it is slightly different 8 00:00:23,070 --> 00:00:24,440 than a regular function, 9 00:00:24,440 --> 00:00:25,900 and so we call it a method. 10 00:00:25,900 --> 00:00:29,500 So I'm going to create a sample class here called 'MyClass'. 11 00:00:29,560 --> 00:00:31,810 It's going to have an attribute, 12 00:00:31,820 --> 00:00:35,290 a default attribute, 'course = "Python for Everybody", 13 00:00:36,200 --> 00:00:40,400 and 'def __init__(self)' 14 00:00:40,400 --> 00:00:42,900 I'm not going to use that, not just yet. 15 00:00:43,500 --> 00:00:45,000 So technically, I didn't need to write that, 16 00:00:45,070 --> 00:00:47,280 but because I did, I put 'pass' in there to basically tell 17 00:00:47,290 --> 00:00:48,720 Python, "Hey, I know it's there. 18 00:00:48,840 --> 00:00:50,070 There's nothing in there. 19 00:00:50,070 --> 00:00:51,200 Just let it be". 20 00:00:51,840 --> 00:00:54,330 And then I'm going to create another method in here, and 21 00:00:54,340 --> 00:00:56,070 this is going to be a completely custom one. 22 00:00:56,140 --> 00:00:58,780 And this is just going to be called 'thing'. 23 00:00:59,140 --> 00:01:01,360 And so its first parameter is going to be 'self'. 24 00:01:01,600 --> 00:01:04,500 First parameter is always going to be 'self', and that's what 25 00:01:04,500 --> 00:01:06,800 connects it to this entire class. 26 00:01:06,879 --> 00:01:10,540 And then it can take an optional parameter, if you want to 27 00:01:10,540 --> 00:01:11,500 give it a parameter. 28 00:01:11,500 --> 00:01:15,500 So I'm going to call this 'param1', and by default it's going 29 00:01:15,510 --> 00:01:20,020 to have a regular value of "Thing", and all this is going to 30 00:01:20,020 --> 00:01:22,000 do is 'print(thing)'. 31 00:01:22,020 --> 00:01:23,310 That's all it's going to do. 32 00:01:23,320 --> 00:01:28,240 So if you look at this, objectively speaking, this looks 33 00:01:28,250 --> 00:01:29,410 exactly like a function. 34 00:01:29,410 --> 00:01:32,600 The only difference is we have to put in 'self'. 35 00:01:32,640 --> 00:01:34,560 So let's go ahead and run this. 36 00:01:34,570 --> 00:01:38,260 And if you were screaming at your screen saying, "Hey, Kalob, 37 00:01:38,380 --> 00:01:40,150 that's almost like a function, 38 00:01:40,180 --> 00:01:42,040 except you missed a thing. 39 00:01:42,040 --> 00:01:43,900 Yep, I did. I missed the colon. 40 00:01:43,980 --> 00:01:45,810 So let's go ahead and run that. 41 00:01:45,810 --> 00:01:47,800 And let's just instantiate this in a variable. 42 00:01:47,800 --> 00:01:51,500 'my_class = MyClass()'. 43 00:01:51,500 --> 00:01:52,700 So we actually run the class. 44 00:01:53,700 --> 00:01:54,700 Nothing happens. 45 00:01:54,740 --> 00:01:56,020 It looks like nothing happens. 46 00:01:56,140 --> 00:02:01,300 But if we do 'my_class.thing', well, this is actually looking 47 00:02:01,300 --> 00:02:06,100 for a 'param1', but by default, it's already going to be "Thing". 48 00:02:06,100 --> 00:02:07,400 So it's going to 'print(thing)' in here, 49 00:02:08,400 --> 00:02:09,600 and I actually did that wrong. 50 00:02:09,600 --> 00:02:10,600 That's not supposed to be 'thing'. 51 00:02:10,690 --> 00:02:12,960 I was looking at that while typing the wrong thing. 52 00:02:12,970 --> 00:02:13,860 So let's rerun these. 53 00:02:14,800 --> 00:02:15,800 There we go. It says "Thing". 54 00:02:15,860 --> 00:02:22,280 But if I said 'my_class.thing("Something else")', and I overwrote 55 00:02:22,290 --> 00:02:25,960 that parameter, it's going to overwrite the variable inside 56 00:02:25,970 --> 00:02:27,370 of the method, inside of the function. 57 00:02:29,100 --> 00:02:34,600 Now, where '__init__' comes into play, is we want to set a different 58 00:02:34,640 --> 00:02:35,440 parameter in here. 59 00:02:35,440 --> 00:02:40,000 So let's add the number of students in this class. 60 00:02:40,520 --> 00:02:45,800 And so we're going to say, 'self.students = students'. 61 00:02:46,500 --> 00:02:51,000 And if we create another method in here, 'def get_total_students', 62 00:02:52,900 --> 00:02:54,000 pass in 'self', 63 00:02:54,900 --> 00:02:57,400 we can 'print(self.students)'. 64 00:02:58,700 --> 00:03:01,900 And so what this is doing, is saying when you create this 65 00:03:01,900 --> 00:03:03,700 new class, put in the number of students. 66 00:03:03,700 --> 00:03:07,200 So let's put 100_000. 67 00:03:07,240 --> 00:03:10,450 And so this parameter is going to go into the 'students' variable 68 00:03:10,460 --> 00:03:13,830 here, it's then going to be reassigned to 'self.students', 69 00:03:13,830 --> 00:03:19,300 which is the same as saying 'students = 100_000'. 70 00:03:20,300 --> 00:03:22,700 And because this is an attribute, it's a variable inside 71 00:03:22,700 --> 00:03:24,200 of this particular class, 72 00:03:24,200 --> 00:03:28,740 we have access to 'self.students' in here, and you can actually 73 00:03:28,740 --> 00:03:31,600 see that it just makes that quick connection between 74 00:03:31,600 --> 00:03:33,800 'self.students' here and 'self.students' here. 75 00:03:33,800 --> 00:03:35,300 Just get rid of this one because we don't need that. 76 00:03:35,700 --> 00:03:36,900 And when I run this 77 00:03:36,900 --> 00:03:39,300 [no audio] 78 00:03:39,300 --> 00:03:40,600 'Thing', still works. 79 00:03:40,600 --> 00:03:41,800 That still works. 80 00:03:41,820 --> 00:03:46,210 And if I do 'my_class.get_total_students', 81 00:03:46,400 --> 00:03:50,500 this is going to print out the total number of students. Cool. 82 00:03:50,900 --> 00:03:54,400 And in fact, a better example of this would be using 83 00:03:54,400 --> 00:04:00,500 an f-string. We could say, 'f"The total students is ", we can put this 84 00:04:00,500 --> 00:04:01,500 as a variable. 85 00:04:01,500 --> 00:04:03,200 Let's save that. Rerun these. 86 00:04:03,200 --> 00:04:06,300 And now it says, "The total student is: 100000". 87 00:04:06,360 --> 00:04:09,930 Now the reason I did that, is because we can also now use 88 00:04:09,940 --> 00:04:12,930 'self.students' from 'MyClass', or the variable. 89 00:04:12,930 --> 00:04:15,000 So now we're jumping around from point to point. 90 00:04:15,000 --> 00:04:18,800 We're really referencing a lot here, but we can now easily 91 00:04:18,800 --> 00:04:20,600 say 'my_class.', 92 00:04:21,100 --> 00:04:23,600 what was that variable name? Just 'students'. 93 00:04:23,600 --> 00:04:26,300 [no audio] 94 00:04:26,300 --> 00:04:28,700 And that gives me just a 100000, because that's 95 00:04:28,700 --> 00:04:29,700 what I put up here. 96 00:04:30,700 --> 00:04:35,000 So whenever you are using methods or functions inside of a 97 00:04:35,000 --> 00:04:37,200 class, it looks exactly like a function. 98 00:04:37,200 --> 00:04:41,200 So 'def', your function name, always take 'self', 99 00:04:41,200 --> 00:04:43,100 and that's what's going to connect it to the rest of the 100 00:04:43,100 --> 00:04:46,600 class, that gives it access to run 'self.thing' 101 00:04:46,600 --> 00:04:49,700 if we wanted to, or get 'self.course', or 'self.students', that's 102 00:04:49,750 --> 00:04:51,390 what connects it to this class. 103 00:04:52,400 --> 00:04:53,800 And then parameter. 104 00:04:53,830 --> 00:04:56,700 If we wanted to work with a parameter, we could force it 105 00:04:56,710 --> 00:05:00,360 to be in there, and then just do something in here, any sort 106 00:05:00,370 --> 00:05:02,940 of logic, it could print something, it could return something, 107 00:05:02,940 --> 00:05:05,000 it could even change 'students'. 108 00:05:05,000 --> 00:05:09,500 And actually, in fact, now that I think about it, that is a very good idea. 109 00:05:09,500 --> 00:05:15,800 Let's go ahead, and update this with 'def add_student'. 110 00:05:16,800 --> 00:05:17,800 It always takes 'self'. 111 00:05:17,800 --> 00:05:25,600 And we're going to say 'self.students = self.students + 1'. 112 00:05:25,600 --> 00:05:32,400 And let's print this out as, "New total is : ". 113 00:05:32,400 --> 00:05:34,600 [no audio] 114 00:05:35,600 --> 00:05:37,900 Let's run that, run that, run that, run 115 00:05:37,900 --> 00:05:40,650 that, just rerunning all my old cells here. 116 00:05:42,100 --> 00:05:44,700 'total_students', we're currently at 100000. 117 00:05:44,720 --> 00:05:48,840 But if we do 'my_class.', and then hit 'Tab', we can see 118 00:05:48,850 --> 00:05:50,040 we can add a student, 'add_student' in here. 119 00:05:50,800 --> 00:05:51,900 That's a method, 120 00:05:51,900 --> 00:05:53,500 so it takes parentheses. 121 00:05:53,540 --> 00:05:56,180 And again, it does not need to have 'self' in there. 122 00:05:56,190 --> 00:05:59,780 'self' only references that part, the class itself, 123 00:05:59,790 --> 00:06:02,720 and Python magically does that for us behind the scenes. 124 00:06:02,730 --> 00:06:07,080 So run 'my_class.add_student', and we could run this over and 125 00:06:07,090 --> 00:06:08,040 over and over again. 126 00:06:08,040 --> 00:06:11,000 And this will continue to add the number of students for us. 127 00:06:12,700 --> 00:06:14,800 And then, if we just wanted to double check, we could do 128 00:06:14,800 --> 00:06:16,000 'my_class.students', 129 00:06:16,000 --> 00:06:20,400 and this is going to show us 100006 students, just like that. 130 00:06:20,400 --> 00:06:21,400 [no audio]