1 00:00:00,000 --> 00:00:03,690 You now have a class with attributes, a constructor 2 00:00:03,750 --> 00:00:07,470 and methods, great. But this is just a structure 3 00:00:07,470 --> 00:00:10,710 inside your program. If you run your code now like 4 00:00:10,710 --> 00:00:12,990 this, well, nothing is going to happen. So 5 00:00:12,990 --> 00:00:17,610 actually, let's do that. Let's do Run, and code, so 6 00:00:17,610 --> 00:00:21,330 oop_example. Well, this works. But we have 7 00:00:21,360 --> 00:00:25,050 nothing. Okay? If I add, let's say, an add here, 8 00:00:25,050 --> 00:00:27,720 [No audio] 9 00:00:27,720 --> 00:00:32,850 print, say, test, okay, with zero indentation, I 10 00:00:32,850 --> 00:00:37,200 run it, and I see test. Okay, so test is executed 11 00:00:37,470 --> 00:00:41,340 the class, well, nothing happened. So just as we 12 00:00:41,340 --> 00:00:43,530 have seen with a function, well, a function is 13 00:00:43,530 --> 00:00:46,170 just a bunch of code that you need to call in your 14 00:00:46,170 --> 00:00:50,370 program, otherwise, it won't do anything. And this 15 00:00:50,370 --> 00:00:53,010 is the same for classes, we need to create an 16 00:00:53,010 --> 00:00:56,670 object or also called an instance of the class, 17 00:00:56,820 --> 00:00:59,730 so we can use the class functionalities. So let's 18 00:00:59,730 --> 00:01:03,030 actually do this, I'm going to create an object 19 00:01:03,030 --> 00:01:07,680 here. And I'm going to call it my_robot is equal 20 00:01:07,680 --> 00:01:12,210 to Robot like this. And I open the parenthesis, and 21 00:01:12,210 --> 00:01:14,880 as you can see, so I have the suggestions from 22 00:01:15,180 --> 00:01:19,080 PyCharm. So this, those here are actually the 23 00:01:19,080 --> 00:01:23,340 parameters from the constructor here. So we need 24 00:01:23,340 --> 00:01:26,160 to provide the name and the version_number. So 25 00:01:26,160 --> 00:01:32,010 let's say our Robot is named R2D2. And the 26 00:01:32,010 --> 00:01:35,450 version_number is, so here, well, for the version_number, 27 00:01:35,450 --> 00:01:37,710 you could use an integer, float, string, 28 00:01:37,710 --> 00:01:40,170 whatever. And I'm just going to keep it very 29 00:01:40,170 --> 00:01:43,110 simple, and say this is the version two of the 30 00:01:43,410 --> 00:01:47,730 Robot. So now I have my_robot. Okay, so to 31 00:01:47,730 --> 00:01:51,540 create an object, you call the constructor simply 32 00:01:51,540 --> 00:01:55,470 by as you can see, writing the name of the class, 33 00:01:55,560 --> 00:01:58,920 and then parentheses. And if there are any 34 00:01:58,920 --> 00:02:02,910 parameters here after self, you then need to 35 00:02:02,910 --> 00:02:07,346 provide the parameters. So name, and version_number, 36 00:02:07,346 --> 00:02:09,449 what's going to happen is that the name 37 00:02:09,479 --> 00:02:12,210 here, this is going to be a local variable, that 38 00:02:12,210 --> 00:02:16,170 is going to be assigned in the self.name, so in 39 00:02:16,170 --> 00:02:18,390 the attribute of the class, and the same for 40 00:02:18,390 --> 00:02:21,960 version_number, okay. So after we call this, this 41 00:02:21,960 --> 00:02:24,690 is going to be self dot name, this is going to be 42 00:02:24,690 --> 00:02:27,750 self.version_number in the class. And now what 43 00:02:27,750 --> 00:02:30,450 I can do, so here, it's not going to do anything, 44 00:02:30,450 --> 00:02:33,690 just create the Robot with the constructor. And 45 00:02:33,690 --> 00:02:39,956 then what I can do, for example, is my_robot.say_hi, 46 00:02:39,956 --> 00:02:45,453 okay, and then my_robot.init_hardware, 47 00:02:45,453 --> 00:02:52,440 and then my_robot.print_info. Okay, 48 00:02:52,440 --> 00:02:55,170 so as you can see here, so the self, the first 49 00:02:55,170 --> 00:02:57,480 parameter is actually not something that you have 50 00:02:57,480 --> 00:03:01,170 to add here, the self refers to the object. So if 51 00:03:01,170 --> 00:03:03,480 you have this, it means you have to call the 52 00:03:03,480 --> 00:03:08,635 method directly from the object. So my_robot.say_hi, 53 00:03:08,635 --> 00:03:13,800 it's going simply to call this and pass 54 00:03:13,890 --> 00:03:17,160 the my_robot object as the self here, as simple 55 00:03:17,160 --> 00:03:19,560 as that. And then if you have any other parameter, 56 00:03:19,560 --> 00:03:22,560 you call the parameter. But otherwise, that's it. 57 00:03:23,100 --> 00:03:27,540 So I'm going to run the test. So I create an 58 00:03:27,600 --> 00:03:30,300 object, and then I call the different functions or 59 00:03:30,300 --> 00:03:33,780 the different methods. And then I can also get 60 00:03:33,810 --> 00:03:37,590 access. So let's say print, I can get also access 61 00:03:37,590 --> 00:03:43,170 to my_robot, for example, dot, let's say name. So 62 00:03:43,170 --> 00:03:47,010 you can get access of the attributes, okay, of the 63 00:03:47,010 --> 00:03:51,090 class, also directly with my_robot.name. Let's 64 00:03:51,120 --> 00:03:55,980 run this now. And let's see what we have. So we 65 00:03:55,980 --> 00:03:59,363 first create the Robot. And then you can see say_hi 66 00:03:59,363 --> 00:04:02,370 hi is going to say Hello, my name is R2D2 67 00:04:02,370 --> 00:04:06,551 ready to help. Actually, I'm just going to add these here. 68 00:04:08,280 --> 00:04:11,910 And then Init hardware, okay, because we call it init_hardware method. 69 00:04:11,910 --> 00:04:14,790 And then you can see we call print_info, 70 00:04:14,790 --> 00:04:17,100 which is going to call say_hi, another time, 71 00:04:17,100 --> 00:04:19,829 so hello again. And then Version number:, 72 00:04:19,860 --> 00:04:22,980 Temperature, which is from the print_info method. 73 00:04:23,250 --> 00:04:26,790 And then we'll print my_robot.name, which is 74 00:04:26,790 --> 00:04:29,010 going to print R2D2, which is the 75 00:04:29,370 --> 00:04:32,430 attribute of the class directly. Okay, nice. Now 76 00:04:32,430 --> 00:04:35,280 we have an object created from the Robot class. 77 00:04:35,430 --> 00:04:38,790 But actually, we can create as many objects as we 78 00:04:38,790 --> 00:04:41,700 want from the class. Okay? Just as when you create 79 00:04:41,700 --> 00:04:44,130 a function, you can just call it as many times as 80 00:04:44,130 --> 00:04:46,920 you want, here, I'm going to create another Robot, 81 00:04:46,920 --> 00:04:52,200 let's say my_robot2. Okay. And then my_robot2 82 00:04:52,200 --> 00:04:55,950 is going to be Robot, also. So the constructor and 83 00:04:55,950 --> 00:05:00,298 I'm going to give a different name. Let's say C3PO, 84 00:05:00,298 --> 00:05:02,280 and this is going to be Version 85 00:05:02,280 --> 00:05:07,680 one of the Robot. And I'm going to remove that 86 00:05:08,160 --> 00:05:10,405 and remove that. And I'm simply going to do my 87 00:05:10,405 --> 00:05:15,574 my_robot.print_info and my_robot2.print_info. 88 00:05:15,574 --> 00:05:17,940 Okay, so I call the same method from 89 00:05:17,970 --> 00:05:21,420 two different objects, which have two different 90 00:05:21,420 --> 00:05:25,500 sets of attributes. Okay, I run this, and you can 91 00:05:25,500 --> 00:05:28,680 see, Hello, my name is R2D2, Version 92 00:05:28,680 --> 00:05:33,530 number: 2, Temperature 25. And then my name is C3PO, 93 00:05:33,530 --> 00:05:36,480 and Version number: 1. Okay, so here 94 00:05:36,480 --> 00:05:40,170 we have two complete independent and different 95 00:05:40,230 --> 00:05:42,840 objects, okay, using the same class, the same 96 00:05:42,840 --> 00:05:45,630 blueprint, but that have a different set of 97 00:05:45,660 --> 00:05:48,120 attributes, okay, and you can use them 98 00:05:48,180 --> 00:05:51,600 independently. So to recap, in order to create an 99 00:05:51,600 --> 00:05:54,090 object from a class, you need to write the name of 100 00:05:54,090 --> 00:05:58,110 the class first, to call the constructor and pass 101 00:05:58,110 --> 00:06:01,530 the required parameters to initialize attributes. 102 00:06:01,920 --> 00:06:04,410 Then from that object, you can call the class 103 00:06:04,410 --> 00:06:07,680 methods. You can also create multiple objects from 104 00:06:07,680 --> 00:06:11,130 the same class, and each object will have its own 105 00:06:11,160 --> 00:06:15,000 internal mechanism and attributes independent from 106 00:06:15,030 --> 00:06:17,174 the other objects.