1 00:00:00,000 --> 00:00:05,000 You now have a class with attributes, a constructor and methods. Great! 2 00:00:05,001 --> 00:00:09,000 But this is just a structure inside your program. 3 00:00:09,001 --> 00:00:13,000 If you run your code now like this, well nothing is going to happen. 4 00:00:13,001 --> 00:00:17,000 So actually let's do that. Let's do run and code. 5 00:00:17,001 --> 00:00:22,000 So, for example. Well, this works but we have nothing. 6 00:00:22,001 --> 00:00:33,000 If I add, let's say I add here print, let's say test, with zero indentation, 7 00:00:33,001 --> 00:00:36,244 I run it and I see tests. So the test is 8 00:00:36,256 --> 00:00:40,000 executed but the class, well nothing happened. 9 00:00:40,001 --> 00:00:45,000 So just as we have seen with a function, well a function is just a bunch of code 10 00:00:45,001 --> 00:00:50,000 that you need to call in your program. Otherwise it won't do anything. 11 00:00:50,001 --> 00:00:54,000 And this is the same for classes. We need to create an object, 12 00:00:54,001 --> 00:00:59,000 also called an instance of the class, so we can use the class functionalities. 13 00:00:59,001 --> 00:01:02,945 So let's actually do this. I'm going to create 14 00:01:02,957 --> 00:01:07,000 an object here and I'm going to call it myRobot 15 00:01:07,001 --> 00:01:13,000 is equal to robot, like this, and I open the parentheses. And as you can see, 16 00:01:13,001 --> 00:01:16,368 so I have the suggestions from PyCharm, so 17 00:01:16,380 --> 00:01:20,000 these, those here are actually the parameters 18 00:01:20,001 --> 00:01:25,000 from the constructor here. So I need to provide a name and a version number. 19 00:01:25,001 --> 00:01:34,000 So let's say our robot is named R2D2 and the version number is, 20 00:01:34,001 --> 00:01:35,849 so here, well for the version number you 21 00:01:35,861 --> 00:01:38,000 could use an integer, float, string, whatever, 22 00:01:38,001 --> 00:01:44,000 and I'm just going to keep it very simple and say this is the version 2 of the robot. 23 00:01:44,001 --> 00:01:52,000 So now I have my robot. So to create an object, you call the constructor simply by, 24 00:01:52,001 --> 00:01:55,989 as you can see, writing the name of the class and 25 00:01:56,001 --> 00:02:00,000 then parentheses. And if there are any parameters 26 00:02:00,001 --> 00:02:07,000 here after self, you then need to provide the parameters, so name and version number. 27 00:02:07,001 --> 00:02:10,050 What's going to happen is that the name here, this 28 00:02:10,062 --> 00:02:13,000 is going to be a local variable that is going to 29 00:02:13,001 --> 00:02:16,116 be assigned in the self name, so in the attribute 30 00:02:16,128 --> 00:02:19,000 of the class and the same for version number. 31 00:02:19,001 --> 00:02:26,000 So after we call this, this is going to be self.name, this is going to be self.version 32 00:02:26,001 --> 00:02:28,462 number in the class. And now what I can do, so 33 00:02:28,474 --> 00:02:31,000 here it's not going to do anything, just create 34 00:02:31,001 --> 00:02:35,703 the robot with the constructor and then what 35 00:02:35,715 --> 00:02:40,000 I can do, for example, is my robot.sayI, 36 00:02:40,001 --> 00:02:51,000 okay, and then my robot.init_hardware and then my robot.print_info. 37 00:02:51,001 --> 00:02:53,989 Okay, so as you can see here, so the self, the 38 00:02:54,001 --> 00:02:57,000 first parameter is actually not something that 39 00:02:57,001 --> 00:03:00,172 you have to add here, the self refers to the object, 40 00:03:00,184 --> 00:03:03,000 so if you have this, it means you have to call 41 00:03:03,001 --> 00:03:07,903 the method directly from the object. So my 42 00:03:07,915 --> 00:03:14,000 robot.sayHi, it's going simply to call this and pass 43 00:03:14,001 --> 00:03:16,411 the my robot object as the self here, as simple 44 00:03:16,423 --> 00:03:19,000 as that. And then if you have any other parameter, 45 00:03:19,001 --> 00:03:24,000 you call the parameter, but otherwise that's it. I'm going to remove the test. 46 00:03:26,000 --> 00:03:28,372 So I create an object and then I call the 47 00:03:28,384 --> 00:03:31,000 different functions or the different methods. 48 00:03:31,001 --> 00:03:35,130 And then I can also get access, so let's say 49 00:03:35,142 --> 00:03:39,000 print, I can get also access to my robot, 50 00:03:39,001 --> 00:03:42,989 for example, dot, let's say name. And so you can 51 00:03:43,001 --> 00:03:47,000 get access of the attributes, okay, of the class 52 00:03:47,001 --> 00:03:54,000 also directly with my robot.name. Let's run this now and let's see what we have. 53 00:03:55,000 --> 00:03:58,568 So we first create the robot and then you can 54 00:03:58,580 --> 00:04:02,000 see sayHi is going to say hello, my name is 55 00:04:02,001 --> 00:04:05,857 R2D2 ready to help. Actually, I'm just going 56 00:04:05,869 --> 00:04:10,000 to add this here. And then init hardware, okay, 57 00:04:10,001 --> 00:04:12,410 because we call init hardware method. And then 58 00:04:12,422 --> 00:04:15,000 you can see we call print_info, which is going to 59 00:04:15,001 --> 00:04:18,637 call sayHi another time, so hello again. And then 60 00:04:18,649 --> 00:04:22,000 version number temperature, which is from the 61 00:04:22,001 --> 00:04:25,092 print_info method. And then, well, print my 62 00:04:25,104 --> 00:04:29,000 robot.name, which is going to print R2D2, which is the 63 00:04:29,001 --> 00:04:32,020 attribute of the class directly. Okay, nice. Now 64 00:04:32,032 --> 00:04:35,000 we have an object created from the robot class. 65 00:04:35,001 --> 00:04:38,453 But actually, we can create as many objects as we 66 00:04:38,465 --> 00:04:42,000 want from the class, okay. Just as when you create 67 00:04:42,001 --> 00:04:44,010 a function, you can just call it as many times 68 00:04:44,022 --> 00:04:46,000 as you want here. I'm going to create another 69 00:04:46,001 --> 00:04:51,295 robot. Let's say my robot2, okay. And then my robot2 70 00:04:51,307 --> 00:04:56,000 is going to be robot also. So the constructor, 71 00:04:56,001 --> 00:04:59,525 and I'm going to give a different name. Let's say 72 00:04:59,537 --> 00:05:03,000 C3PO, and this is going to be version one of the 73 00:05:03,001 --> 00:05:07,213 robot. And I'm going to remove that and remove 74 00:05:07,225 --> 00:05:12,000 that. And I'm simply going to do my robot.print_info 75 00:05:12,001 --> 00:05:16,252 and my robot2.print_info. Okay, so I call the 76 00:05:16,264 --> 00:05:21,000 same method from two different objects, which have 77 00:05:21,001 --> 00:05:24,450 two different sets of attributes. Okay, I run 78 00:05:24,462 --> 00:05:28,000 this. And you can see, hello, my name is R2D2, 79 00:05:28,001 --> 00:05:31,944 version number two, temperature 25. And then 80 00:05:31,956 --> 00:05:36,000 my name is C3PO and version number one. Okay, 81 00:05:36,001 --> 00:05:38,922 so here we have two complete independent and 82 00:05:38,934 --> 00:05:42,000 different objects, okay, using the same class, 83 00:05:42,001 --> 00:05:44,989 the same blueprint, but that have a different 84 00:05:45,001 --> 00:05:48,000 set of attributes, okay. And you can use them 85 00:05:48,001 --> 00:05:51,051 independently. So to recap, in order to create an 86 00:05:51,063 --> 00:05:54,000 object from a class, you need to write the name 87 00:05:54,001 --> 00:05:57,329 of the class first to call the constructor 88 00:05:57,341 --> 00:06:01,000 and pass the required parameters to initialize 89 00:06:01,001 --> 00:06:03,989 attributes. Then from that object, you can call 90 00:06:04,001 --> 00:06:07,000 the class methods. You can also create multiple 91 00:06:07,001 --> 00:06:10,599 objects from the same class. And each object will 92 00:06:10,611 --> 00:06:17,000 have its own internal mechanism and attributes independent from the other objects.