1 00:00:00,000 --> 00:00:02,700 Now that we have the structure of the class, we have 2 00:00:02,700 --> 00:00:05,730 the constructor and the attributes, let's 3 00:00:05,760 --> 00:00:08,730 implement the methods of the class or in other 4 00:00:08,730 --> 00:00:12,930 words, the functionalities of the class. And to 5 00:00:12,930 --> 00:00:15,510 create a method, well, it's almost the same thing 6 00:00:15,540 --> 00:00:17,910 as to create a function like you did previously. 7 00:00:18,060 --> 00:00:20,250 And we have already actually our first method, 8 00:00:20,250 --> 00:00:22,590 which is the constructor. Now we can add any 9 00:00:22,590 --> 00:00:26,580 number of methods we want in the class, so still 10 00:00:26,580 --> 00:00:29,940 with the indentation to stay inside of the class, 11 00:00:29,940 --> 00:00:32,970 okay, and then def. And then I want to implement 12 00:00:33,000 --> 00:00:37,890 the say_hi, method, okay. And as you can see, so 13 00:00:37,890 --> 00:00:41,250 this is functionality of PyCharm. Whenever I 14 00:00:41,250 --> 00:00:44,070 create a function or method inside a class, I 15 00:00:44,070 --> 00:00:47,160 automatically get the self parameter. If you don't 16 00:00:47,160 --> 00:00:50,490 get it, you simply write it, okay, so self, and we 17 00:00:50,490 --> 00:00:53,760 don't want any other parameter. So as you can see, 18 00:00:53,790 --> 00:00:57,930 the first parameter of any method would be self. 19 00:00:57,990 --> 00:01:02,010 Okay, so we have the reference to the objects we 20 00:01:02,010 --> 00:01:05,129 are currently using. So the method is really 21 00:01:05,129 --> 00:01:07,830 linked, okay, to the object and to the class 22 00:01:07,860 --> 00:01:10,350 Robot, it's not just a function, you can call from 23 00:01:10,380 --> 00:01:13,170 anywhere. This is a specific function that is 24 00:01:13,170 --> 00:01:15,600 directly linked to Robot, and you can only call 25 00:01:15,600 --> 00:01:20,060 it in relation to the Robot class. Okay, so say_hi, 26 00:01:20,060 --> 00:01:23,670 let's say we want to do so print. Hello, my 27 00:01:23,670 --> 00:01:29,280 name is and then let's do self.name. So as you 28 00:01:29,280 --> 00:01:31,800 can see, with the self keyword here, I can use 29 00:01:31,800 --> 00:01:35,420 self.name. And because I have created self.name, 30 00:01:35,420 --> 00:01:37,650 this is going to work here, okay, because I 31 00:01:37,650 --> 00:01:41,100 have initialized the variable or the attributes, 32 00:01:41,130 --> 00:01:44,730 okay. And then I can use it in any other method, 33 00:01:44,970 --> 00:01:48,630 okay, and then plus, let's go back to a new line 34 00:01:48,660 --> 00:01:56,395 here. Let's do ready to help. So that's the say_hi 35 00:01:56,395 --> 00:02:00,300 method. And then I'm going to come back. So 36 00:02:00,690 --> 00:02:05,850 come back to the same indentation here. And do def 37 00:02:06,030 --> 00:02:11,460 init_hardware. So again, we just need self, 38 00:02:11,760 --> 00:02:15,510 I'm not going to add any other parameter. Okay, 39 00:02:15,510 --> 00:02:19,290 colon, actually, it's better if I end the print, 40 00:02:19,290 --> 00:02:21,780 with the parenthesis here, and here, because this 41 00:02:21,780 --> 00:02:28,463 is just an example, I'm going to do print init_hardware, okay, 42 00:02:28,463 --> 00:02:31,170 just going to use a print key in 43 00:02:31,170 --> 00:02:33,810 real life, you would actually implement this 44 00:02:33,810 --> 00:02:36,510 method to init the real hardware, here we don't have 45 00:02:36,510 --> 00:02:40,110 any hardware, just an example. So the point is to 46 00:02:40,110 --> 00:02:43,770 understand our OOP, so let's use the print here. So 47 00:02:43,770 --> 00:02:45,870 when we call that method, we can see that we have 48 00:02:45,900 --> 00:02:49,290 actually called it and that it works. And then I'm 49 00:02:49,290 --> 00:02:51,930 going to create another one def, let's say 50 00:02:51,930 --> 00:02:56,220 print_info. Okay, again, we've self, I don't need 51 00:02:56,250 --> 00:03:00,480 any other parameter. And what I'm going to do here 52 00:03:00,510 --> 00:03:03,060 is I'm going to print all the info, okay, which is 53 00:03:03,060 --> 00:03:06,194 the name, the version_number and internal_temperature. 54 00:03:06,194 --> 00:03:10,942 So I'm going first to do self.say_hi, 55 00:03:10,942 --> 00:03:13,980 okay. As you can see, I can directly call 56 00:03:14,160 --> 00:03:16,950 the method of the class from another method of the 57 00:03:16,950 --> 00:03:21,428 class, okay? And to do that, I simply do self.say_hi, 58 00:03:21,428 --> 00:03:23,130 okay, if I just called say_hi, it's 59 00:03:23,130 --> 00:03:26,100 not going to work because the function say_hi 60 00:03:26,100 --> 00:03:30,030 doesn't exist globally. But the method say_hi, 61 00:03:30,120 --> 00:03:34,114 exists inside the class Robot. So if I do self., 62 00:03:34,114 --> 00:03:37,170 I can get access to the different methods and 63 00:03:37,230 --> 00:03:40,380 attributes here. Okay. So you can see how to call 64 00:03:40,530 --> 00:03:43,020 the method from another method. And then let's say 65 00:03:43,020 --> 00:03:52,230 print. So Version number: plus self.version_number, 66 00:03:52,230 --> 00:03:57,540 I'm going to cast this as a string, because this 67 00:03:57,540 --> 00:04:01,080 is probably going to be integer float, that can be 68 00:04:01,080 --> 00:04:03,630 a string, but we don't know. Okay, the name here 69 00:04:03,630 --> 00:04:05,880 is probably going to be a string, so I don't need 70 00:04:05,880 --> 00:04:10,050 to cast it. But here let's take some precaution, and 71 00:04:10,050 --> 00:04:17,220 then print temperature plus, and then the same self 72 00:04:17,220 --> 00:04:21,810 internal_temperature. Okay, so in this method, 73 00:04:21,810 --> 00:04:24,000 you can see we've got another method and we use 74 00:04:24,000 --> 00:04:26,850 the different attributes. Okay, so self, and the 75 00:04:26,850 --> 00:04:29,370 attributes. If you want, you can also modify the 76 00:04:29,370 --> 00:04:32,010 attributes, okay. You just do self.version_number 77 00:04:32,040 --> 00:04:35,520 is equal to and they just change it. Okay? That's 78 00:04:35,520 --> 00:04:38,940 not the problem. So now we have a constructor, we 79 00:04:38,940 --> 00:04:41,640 have three attributes, and we also have three 80 00:04:41,790 --> 00:04:44,760 methods inside the class. All right, we have 81 00:04:44,760 --> 00:04:47,070 successfully written all the functionalities of 82 00:04:47,070 --> 00:04:49,560 the class inside different methods. And now let's 83 00:04:49,560 --> 00:04:50,922 go to the next step.