1 00:00:00,000 --> 00:00:05,000 Now that we have the structure of the class with the constructor and the attributes, 2 00:00:05,001 --> 00:00:08,489 let's implement the methods of the class or in 3 00:00:08,501 --> 00:00:12,000 other words, the functionalities of the class. 4 00:00:12,001 --> 00:00:14,873 And to create a method, well, it's almost the same 5 00:00:14,885 --> 00:00:18,000 thing as to create a function like you did previously, 6 00:00:18,001 --> 00:00:21,000 and we have already actually our first method, which is the constructor. 7 00:00:21,001 --> 00:00:26,000 Now we can add any number of methods we want in the class, 8 00:00:26,001 --> 00:00:31,000 so still with the indentation to stay inside of the class, and then def, 9 00:00:31,001 --> 00:00:37,000 and then I want to implement the sayHi method. 10 00:00:37,001 --> 00:00:41,000 And as you can see, so this is a functionality of PyCharm, 11 00:00:41,001 --> 00:00:43,957 whenever I create a function or a method inside 12 00:00:43,969 --> 00:00:47,000 a class, I automatically get the self parameter. 13 00:00:47,001 --> 00:00:49,000 If you don't get it, you simply write it. 14 00:00:49,001 --> 00:00:53,000 So self and we don't want any other parameter. 15 00:00:53,001 --> 00:00:58,000 So as you can see, the first parameter of any method would be self. 16 00:00:58,001 --> 00:01:03,000 Okay, so we have a reference to the objects we are currently using. 17 00:01:03,001 --> 00:01:08,000 So the method is really linked to the object and to the class robot. 18 00:01:08,001 --> 00:01:11,000 It's not just a function you can call from anywhere. 19 00:01:11,001 --> 00:01:14,000 This is a specific function that is directly linked to robot 20 00:01:14,001 --> 00:01:18,000 and you can only call it in relation to the robot class. 21 00:01:18,001 --> 00:01:22,000 So sayHi, let's say we want to do so print. 22 00:01:22,001 --> 00:01:29,000 Hello, my name is, and then let's do self.name. 23 00:01:29,001 --> 00:01:33,000 So as you can see with the self keyword here, I can use self.name. 24 00:01:33,001 --> 00:01:37,000 And because I have created self.name, this is going to work here, 25 00:01:37,001 --> 00:01:42,000 okay, because I have initialized the variable or the 26 00:01:42,001 --> 00:01:45,000 attributes, okay, and then I can use it in any other method. 27 00:01:45,001 --> 00:01:49,000 Okay, and then plus, let's go back to a new line here. 28 00:01:49,001 --> 00:01:53,000 Let's do ready to help. 29 00:01:53,001 --> 00:01:58,000 Okay, so that's the sayHi method. 30 00:01:58,001 --> 00:02:00,000 And then I'm going to come back. 31 00:02:00,001 --> 00:02:08,000 So come back to the same indentation here and do def init hardware. 32 00:02:08,001 --> 00:02:12,000 So again, we just need self. 33 00:02:12,001 --> 00:02:15,000 I'm not going to add any other parameter. 34 00:02:15,001 --> 00:02:16,000 Okay, colon. 35 00:02:16,001 --> 00:02:21,000 And actually, it's better if I end the print with a parenthesis here. 36 00:02:21,001 --> 00:02:28,000 And here, because this is just an example, I'm going to do print init hardware. 37 00:02:28,001 --> 00:02:31,000 Okay, just going to use a print. 38 00:02:31,001 --> 00:02:33,626 Okay, in real life, you would actually implement 39 00:02:33,638 --> 00:02:36,000 this method to init the real hardware here. 40 00:02:36,001 --> 00:02:38,000 We don't have any hardware, just an example. 41 00:02:38,001 --> 00:02:43,000 So the point is to understand how OP, so let's use a print here. 42 00:02:43,001 --> 00:02:45,460 So when we call that method, we can see that 43 00:02:45,472 --> 00:02:48,000 we have actually called it and that it works. 44 00:02:48,001 --> 00:02:53,000 And then I'm going to create another one, def, let's say print info. 45 00:02:53,001 --> 00:02:58,000 Okay, again, with self, I don't need any other parameter. 46 00:02:58,001 --> 00:03:02,000 And what I'm going to do here is I'm going to print all the info, 47 00:03:02,001 --> 00:03:06,000 okay, which is the name, the version number, and internal temperature. 48 00:03:06,001 --> 00:03:11,000 So I'm going first to do self.sayHi. 49 00:03:11,001 --> 00:03:14,172 Okay, as you can see, I can directly call the method 50 00:03:14,184 --> 00:03:17,000 of the class from another method of the class. 51 00:03:17,001 --> 00:03:21,000 Okay, and to do that, I simply do self.sayHi. 52 00:03:21,001 --> 00:03:24,489 Okay, if I just call sayHi, it's not going to work 53 00:03:24,501 --> 00:03:28,000 because the function sayHi doesn't exist globally. 54 00:03:28,001 --> 00:03:32,000 But the method sayHi exists inside the class robot. 55 00:03:32,001 --> 00:03:38,000 So if I do self. I can get access to the different methods and attributes here. 56 00:03:38,001 --> 00:03:42,000 Okay, so you can see how to call the method from another method. 57 00:03:42,001 --> 00:03:43,000 And then let's say print. 58 00:03:43,001 --> 00:03:56,000 So version number plus self version number, I'm going to cast this as a string, 59 00:03:56,001 --> 00:04:00,000 because this is probably going to be an integer flow. 60 00:04:00,001 --> 00:04:03,000 That can be a string, but we don't know. 61 00:04:03,001 --> 00:04:07,000 Okay, the name here is probably going to be a string, so I don't need to cast it. 62 00:04:07,001 --> 00:04:09,000 But here, let's take some precaution. 63 00:04:09,001 --> 00:04:20,000 And then print temperature plus, and then the same self internal temperature. 64 00:04:20,001 --> 00:04:22,304 Okay, so in this method, you can see we call 65 00:04:22,316 --> 00:04:25,000 another method and we use the different attributes. 66 00:04:25,001 --> 00:04:27,000 Okay, so self and the attributes. 67 00:04:27,001 --> 00:04:30,000 If you want, you can also modify the attributes. 68 00:04:30,001 --> 00:04:35,000 Okay, you just do self version number is equal to, and then you just change it. 69 00:04:35,001 --> 00:04:36,081 Okay, that's not a problem. 70 00:04:37,000 --> 00:04:40,000 So now we have a constructor, we have three attributes, 71 00:04:40,001 --> 00:04:44,000 and we also have three methods inside the class. 72 00:04:44,001 --> 00:04:48,000 All right, we have successfully written all the 73 00:04:48,001 --> 00:04:49,000 functionalities of the class inside different methods. 74 00:04:49,001 --> 00:04:51,000 And now let's go to the next step.