1 00:00:00,000 --> 00:00:03,360 And one last thing I want to show you about Python 2 00:00:03,390 --> 00:00:07,500 OOP is the concept of composition. Let's say that 3 00:00:07,500 --> 00:00:10,770 now you have to work with a packaging solution. 4 00:00:11,100 --> 00:00:13,770 This packaging solution is nothing more than two 5 00:00:13,770 --> 00:00:17,220 robotic arms assembled together to work on an 6 00:00:17,250 --> 00:00:20,520 assembly line. What you can do here is to create a 7 00:00:20,520 --> 00:00:28,121 class, create a new class here, class PackagingSolution, 8 00:00:28,121 --> 00:00:31,080 and inside this class, you're going to 9 00:00:31,080 --> 00:00:35,880 keep and use two instances of the RoboticArm 10 00:00:35,940 --> 00:00:38,790 class. This is what we call composition. 11 00:00:39,060 --> 00:00:41,880 Basically, when you use an object, okay, from 12 00:00:41,880 --> 00:00:45,930 another class, inside of a class, okay, so let's 13 00:00:45,990 --> 00:00:48,960 see with this example. I'm going to create a 14 00:00:48,960 --> 00:00:53,580 constructor here. So def __init__, and I'm going to 15 00:00:53,580 --> 00:00:57,000 pass it, so let's say we have the robotic arm on 16 00:00:57,000 --> 00:00:59,400 the right, and the robotic arm on the left. I'm 17 00:00:59,400 --> 00:01:05,280 going to name it robot_right, and robot_left, 18 00:01:06,000 --> 00:01:13,451 and self.robot_right is equal to robot_right, 19 00:01:13,451 --> 00:01:21,508 and then self.robot_left is equal to robot_left. 20 00:01:21,508 --> 00:01:24,450 So I create two attributes of the 21 00:01:24,450 --> 00:01:28,350 class which are going to be two objects from the 22 00:01:28,350 --> 00:01:32,220 RoboticArm class. And then I create a new method 23 00:01:32,250 --> 00:01:37,110 def, let's say, package, okay. We are going to add 24 00:01:37,110 --> 00:01:40,980 some parameters. So let's say pick_x and then 25 00:01:41,010 --> 00:01:47,880 pick_y, and place_x, and place_y, ok the co-ordinates. Next, 26 00:01:47,880 --> 00:01:51,090 we want to pick and place the object from and to. 27 00:01:51,750 --> 00:01:57,120 And I'm going to also add middle_x, and middle_y. 28 00:01:57,150 --> 00:02:00,630 Okay, so I'm going to make the two robotic arm 29 00:02:00,660 --> 00:02:03,840 collaborates between each other. Okay. So what I 30 00:02:03,840 --> 00:02:12,176 can do, for example, here is do self.robot_right.pick_objects, 31 00:02:12,176 --> 00:02:19,677 Okay, we've pick_x, and pick_y, 32 00:02:19,677 --> 00:02:26,596 and then sel.robot_right.place_objects, 33 00:02:26,596 --> 00:02:30,360 with let's say, middle_x and 34 00:02:30,540 --> 00:02:36,420 middle_y. And then self.robot_left this 35 00:02:36,420 --> 00:02:41,220 time, which is going to pick the object from the 36 00:02:41,220 --> 00:02:50,900 middle_x and middle_y. And then self.robot_left.place_objects 37 00:02:50,900 --> 00:02:54,870 to place_x and place_y. So 38 00:02:54,870 --> 00:02:57,570 basically, what I do is, I make the robot_right, 39 00:02:57,630 --> 00:02:59,640 pick the object and place it in the middle 40 00:02:59,640 --> 00:03:02,760 position. And then the robot_left is going to pick 41 00:03:02,760 --> 00:03:05,220 the object from that middle position and place it 42 00:03:05,550 --> 00:03:09,000 in the place position. Okay, so that's just for 43 00:03:09,000 --> 00:03:11,280 the example so you can see that we use the 44 00:03:11,310 --> 00:03:14,730 instances of that class inside that class. So now 45 00:03:14,730 --> 00:03:18,240 this is ready, what I can do is go back here, and 46 00:03:18,240 --> 00:03:23,867 I'm going to do from robot import PackagingSolution, 47 00:03:23,867 --> 00:03:27,540 and I'm going to add a new robotic arm. 48 00:03:27,570 --> 00:03:34,590 So let's say robotic_arm_left, I'm going to call it 49 00:03:34,590 --> 00:03:43,800 left, and this one robotic_right. Okay, this is 50 00:03:43,800 --> 00:03:47,910 going to be RoboticArm, let's call it so Bob and 51 00:03:47,910 --> 00:03:54,930 John, version 2, reach let's put 300 also. Then 52 00:03:54,930 --> 00:03:58,350 I'm going to remove that. I'm going to comment 53 00:03:58,410 --> 00:04:03,687 that, and what I can do now is I create a PackagingSolution. 54 00:04:03,687 --> 00:04:07,950 So packaging_solution is equal to 55 00:04:08,460 --> 00:04:12,570 PackagingSolution, okay. And I need to give two 56 00:04:12,870 --> 00:04:16,680 parameters, which are this time, two objects. So 57 00:04:16,740 --> 00:04:20,459 robotic_arm_right first, robotic_arm_right and 58 00:04:20,519 --> 00:04:26,310 robotic_arm_left, right. And so now I have two 59 00:04:26,310 --> 00:04:30,090 objects here. But I also can use the object inside 60 00:04:30,120 --> 00:04:33,270 the PackagingSolution class, which I'm going to 61 00:04:33,270 --> 00:04:36,690 do right. Now I'm going to do packaging_solution 62 00:04:36,690 --> 00:04:40,440 dot package. And then I need to provide six 63 00:04:40,440 --> 00:04:46,230 coordinates. So let's put 1, 2, 3, 4, 5, 64 00:04:46,440 --> 00:04:49,920 and 6. Okay, let's keep it simple, and let's run 65 00:04:49,920 --> 00:04:53,730 that. And you can see Pick object on (1,2), place 66 00:04:53,760 --> 00:04:56,820 object on (3,4), Pick object on (3,4), and 67 00:04:56,820 --> 00:05:01,350 Place object on (5,6). Okay, so this, when you 68 00:05:01,350 --> 00:05:03,870 call package, it's going to call the different 69 00:05:03,870 --> 00:05:07,170 methods from the robot_right and the robot_left we 70 00:05:07,170 --> 00:05:10,650 have created here. Okay, which is going to go 71 00:05:10,680 --> 00:05:15,360 here, and then you can see, print the pick_object 72 00:05:15,360 --> 00:05:21,000 and place_object. Great. So now you have both 73 00:05:21,030 --> 00:05:25,200 used the inheritance here, and the composition 74 00:05:25,230 --> 00:05:29,130 concepts with OOP and Python. To quickly recap, 75 00:05:29,280 --> 00:05:32,340 you can use inheritance when there is a relation 76 00:05:32,340 --> 00:05:35,580 where you can use the verb to be. For example, a 77 00:05:35,580 --> 00:05:39,000 RoboticArm is the Robot, and you can use 78 00:05:39,000 --> 00:05:42,690 composition when there is a relation with the verb 79 00:05:42,810 --> 00:05:47,280 to have. For example, the PackagingSolution has 80 00:05:47,490 --> 00:05:51,450 two robotic arms, can be used composition. Alright, 81 00:05:51,570 --> 00:05:55,520 that's the end of this bonus section on our OOP with Python.