1 00:00:00,000 --> 00:00:03,090 The first thing to do when creating a class is to 2 00:00:03,090 --> 00:00:06,330 actually define what attributes and what methods 3 00:00:06,360 --> 00:00:09,000 you need to put into the class. Well, that's what 4 00:00:09,000 --> 00:00:11,550 we have just done in the previous lesson. So now 5 00:00:11,580 --> 00:00:14,820 we can start the implementation with Python code. 6 00:00:14,970 --> 00:00:18,210 And let's write our first class, Robot. So I have 7 00:00:18,210 --> 00:00:21,150 created a new file here, doesn't really matter for 8 00:00:21,180 --> 00:00:24,540 the name, so for example, and to create a class it 9 00:00:24,540 --> 00:00:27,120 is very simple, you use the class keyword, and 10 00:00:27,120 --> 00:00:30,240 then the name of the class and then colon. So 11 00:00:30,240 --> 00:00:33,150 class keyword name of the class, colon, and for 12 00:00:33,150 --> 00:00:35,040 the name of the class, we are going to use a 13 00:00:35,040 --> 00:00:37,740 different convention than before. Here, you are 14 00:00:37,740 --> 00:00:41,160 going to use an uppercase letter, okay, for each 15 00:00:41,190 --> 00:00:44,010 new word, okay, and if you have different word, 16 00:00:44,310 --> 00:00:46,710 for example, RoboticArm that we're going to see 17 00:00:46,710 --> 00:00:49,650 later, well, you're going to put an uppercase for 18 00:00:49,680 --> 00:00:53,700 each new word. Alright, that's the convention. 19 00:00:54,000 --> 00:00:56,340 And then you press enter, and you can see that we 20 00:00:56,340 --> 00:00:59,670 have a new indentation of four spaces here. So 21 00:00:59,670 --> 00:01:03,270 you're going to write your class directly here 22 00:01:03,300 --> 00:01:05,940 with the indentation, okay, so everything that is 23 00:01:05,940 --> 00:01:08,760 indented is inside the class, everything that is 24 00:01:08,760 --> 00:01:11,850 not indented after that is outside of the class. 25 00:01:12,150 --> 00:01:15,120 And well, the first thing we have to do in this 26 00:01:15,180 --> 00:01:18,900 Python class is to create a constructor. The 27 00:01:18,900 --> 00:01:21,180 constructor is a method. Okay, so that's a 28 00:01:21,180 --> 00:01:24,120 function for a class. So this is a method, which 29 00:01:24,150 --> 00:01:27,420 will be called first when you create an object 30 00:01:27,450 --> 00:01:30,750 from the class. Because remember, a class is just 31 00:01:30,750 --> 00:01:34,140 a structure, it does nothing by itself. You will 32 00:01:34,140 --> 00:01:36,540 need to create some objects, so you can use the 33 00:01:36,540 --> 00:01:39,210 functionalities of the class. And when you create 34 00:01:39,210 --> 00:01:41,670 an object from the class, the first thing that is 35 00:01:41,670 --> 00:01:44,040 called is the constructor. And we're going to see 36 00:01:44,040 --> 00:01:47,340 just a little later how to create an object, now 37 00:01:47,370 --> 00:01:50,550 let's focus on the constructor. So the constructor 38 00:01:50,550 --> 00:01:54,390 will always be the same def. So you create def, 39 00:01:54,390 --> 00:01:57,240 like you just create a function, and then 40 00:01:57,270 --> 00:02:01,672 __init__, 41 00:02:01,672 --> 00:02:03,390 and then you open parentheses, okay, I 42 00:02:03,390 --> 00:02:05,850 have the auto completion here, the first 43 00:02:06,120 --> 00:02:09,600 parameter, you are going to give his self. Okay, I'm 44 00:02:09,600 --> 00:02:12,450 going to explain self just a bit later. And then 45 00:02:12,450 --> 00:02:14,970 you can add different parameters. So basically, 46 00:02:14,970 --> 00:02:17,100 the constructor will be called first when you 47 00:02:17,100 --> 00:02:20,850 create an object. So that can be a good point to 48 00:02:20,880 --> 00:02:23,790 initialize your attributes. And so you're going to 49 00:02:23,790 --> 00:02:26,010 pass the attributes of the class in the 50 00:02:26,040 --> 00:02:28,620 constructor parameters. So for example, we have 51 00:02:28,620 --> 00:02:34,980 name, version_number that we want to give to the 52 00:02:34,980 --> 00:02:38,340 Robot. Okay, I'm going to press Enter here. And 53 00:02:38,370 --> 00:02:40,800 of course, we have a new indentation for this 54 00:02:40,800 --> 00:02:43,470 function. So the function, so the method here is 55 00:02:43,500 --> 00:02:46,350 indented inside the class Robot, and then we have 56 00:02:46,350 --> 00:02:49,110 yet another indentation here. And what I'm going to 57 00:02:49,110 --> 00:02:52,050 do, I'm going to initialize the attributes by 58 00:02:52,050 --> 00:02:57,660 using self.name is equal to name, and then 59 00:02:57,780 --> 00:03:03,779 self.version_number is equal to version_number. 60 00:03:03,779 --> 00:03:09,040 And then I'm going to add self.internal_temperature 61 00:03:09,040 --> 00:03:12,330 is equal to and let's put just an 62 00:03:12,360 --> 00:03:18,150 arbitrary number, let's say 25, as a float. So 63 00:03:18,150 --> 00:03:20,430 here in the constructor of the class, we 64 00:03:20,430 --> 00:03:23,490 initialize the attributes of the class, okay, and 65 00:03:23,490 --> 00:03:25,770 to initialize the attributes, we actually to 66 00:03:25,770 --> 00:03:29,760 create some new variables with self, okay. So, for 67 00:03:29,790 --> 00:03:32,370 each method of the class, you are going to have 68 00:03:32,400 --> 00:03:36,390 first the self parameter. So when you use self, 69 00:03:36,450 --> 00:03:40,500 this actually refers to the object, okay, related 70 00:03:40,560 --> 00:03:43,590 to class Robot. And well, how to make the 71 00:03:43,590 --> 00:03:46,380 difference between a simple variable and an 72 00:03:46,410 --> 00:03:48,930 attribute of the class? Well for an attribute of the 73 00:03:48,930 --> 00:03:51,660 class you're simply going to use that self and to 74 00:03:51,660 --> 00:03:55,740 self., and then for example, name. So here, the 75 00:03:55,740 --> 00:03:59,340 name is the parameter. So this is a local variable 76 00:03:59,340 --> 00:04:01,920 inside this method, okay, if you remember from the 77 00:04:01,920 --> 00:04:06,660 scope. self.name, is actually in the class. 78 00:04:06,690 --> 00:04:09,840 So if I use another method, I'm going to see that 79 00:04:09,870 --> 00:04:12,960 just later, I'm going to be able to retrieve the 80 00:04:12,960 --> 00:04:16,589 self.name because this is inside the class of 81 00:04:16,589 --> 00:04:20,370 inside the object Robot that we have created with 82 00:04:20,399 --> 00:04:23,190 the constructor here. Alright, so to recap, you 83 00:04:23,190 --> 00:04:25,680 create a class with the class keyword and then the 84 00:04:25,680 --> 00:04:29,370 name colon, and then you're going to use different 85 00:04:29,430 --> 00:04:33,120 functions are listed here, indented inside the 86 00:04:33,120 --> 00:04:35,600 class. And the first internal method is the 87 00:04:35,670 --> 00:04:39,360 constructor, which is the init method. All right, 88 00:04:39,390 --> 00:04:41,610 we have now created a class structure for the 89 00:04:41,610 --> 00:04:44,370 Robot, and we have successfully created the 90 00:04:44,400 --> 00:04:48,640 attributes inside the class constructor.