1 00:00:00,000 --> 00:00:06,000 And one last thing I want to show you about Python OOP is the concept of composition. 2 00:00:06,001 --> 00:00:11,000 Let's say that now you have to work with a packaging solution. 3 00:00:11,001 --> 00:00:14,618 This packaging solution is nothing more than two robotic 4 00:00:14,630 --> 00:00:18,000 arms assembled together to work on an assembly line. 5 00:00:18,001 --> 00:00:22,848 What you can do here is to create a class, I'm going 6 00:00:22,860 --> 00:00:28,000 to create a new class here, class, packaging, solution. 7 00:00:28,001 --> 00:00:32,640 And inside this class you're going to keep and 8 00:00:32,652 --> 00:00:37,000 use two instances of the robotic arm class. 9 00:00:37,001 --> 00:00:39,000 This is what we call composition. 10 00:00:39,001 --> 00:00:45,000 Basically when you use an object from another class inside of a class. 11 00:00:45,001 --> 00:00:48,000 So let's see with this example. 12 00:00:48,001 --> 00:00:55,000 I'm going to create a constructor here, so def init, and I'm going to pass it. 13 00:00:55,001 --> 00:00:59,000 So let's say we have a robotic arm on the right and a robotic arm on the left. 14 00:00:59,001 --> 00:01:06,000 I'm going to name it robot right and robot left. 15 00:01:06,001 --> 00:01:14,000 And do self.robot right is equal to robot right. 16 00:01:14,001 --> 00:01:21,000 And then self.robot left is equal to robot left. 17 00:01:21,001 --> 00:01:25,399 So I create two attributes of the class which are 18 00:01:25,411 --> 00:01:30,000 going to be two objects from the robotic arm class. 19 00:01:30,001 --> 00:01:36,000 And then I create a new method def, let's say package. 20 00:01:36,001 --> 00:01:38,000 We are going to add some parameters. 21 00:01:38,001 --> 00:01:47,000 So let's say pick x and then pick y and then place x and place y. 22 00:01:47,001 --> 00:01:52,000 The coordinates we want to pick and place the object from and to. 23 00:01:52,001 --> 00:01:57,000 And I'm going to also add middle x and middle y. 24 00:01:57,001 --> 00:02:03,000 So I'm going to make the two robotic arm collaborates between each other. 25 00:02:03,001 --> 00:02:16,000 So what I can do, for example, I can do self.robot right.pick object with pick x. 26 00:02:18,000 --> 00:02:20,000 And pick y. 27 00:02:20,001 --> 00:02:32,000 And then self.robot right.place object with, let's say middle x and middle y. 28 00:02:32,001 --> 00:02:37,989 And then self.robot left this time, which is going 29 00:02:38,001 --> 00:02:44,000 to pick the object from the middle x and middle y. 30 00:02:44,001 --> 00:02:55,000 And then self.robot left.place object to place x and place y. 31 00:02:55,001 --> 00:02:57,414 So basically what I do is I make the robot right, 32 00:02:57,426 --> 00:03:00,000 pick the object and place it in the middle position. 33 00:03:00,001 --> 00:03:03,489 And then the robot left is going to pick the object from 34 00:03:03,501 --> 00:03:07,000 that middle position and place it in the place position. 35 00:03:07,001 --> 00:03:10,000 Okay, so that's just for the example. 36 00:03:10,001 --> 00:03:14,000 So you can see that we use the instances of that class inside that class. 37 00:03:14,001 --> 00:03:16,000 So now this is ready. 38 00:03:16,001 --> 00:03:24,000 What I can do is go back here and I'm going to do from robot import packaging solution. 39 00:03:24,001 --> 00:03:28,000 And I'm going to add a new robotic arm. 40 00:03:28,001 --> 00:03:34,000 So let's say robotic left. 41 00:03:34,001 --> 00:03:43,000 I'm going to call it left in this one robotic right. 42 00:03:43,001 --> 00:03:45,000 Okay, this is going to be robotic arm. 43 00:03:45,001 --> 00:03:47,000 Let's call it. 44 00:03:47,001 --> 00:03:50,000 So Bob and John. 45 00:03:50,001 --> 00:03:52,000 Version two rage. 46 00:03:52,001 --> 00:03:55,000 Let's put 300 also. 47 00:03:55,001 --> 00:03:57,000 Then I'm going to remove that. 48 00:03:57,001 --> 00:04:00,000 I'm going to comment that. 49 00:04:00,001 --> 00:04:04,000 And what I can do now is I create a packaging solution. 50 00:04:04,001 --> 00:04:10,000 So packaging solution is equal to packaging solution. 51 00:04:10,001 --> 00:04:16,000 Okay, and I need to give two parameters which are this time two objects. 52 00:04:16,001 --> 00:04:25,000 So robotic arm right first robotic arm right and robotic arm left right. 53 00:04:25,001 --> 00:04:28,454 And so now I have two objects here, but I also can 54 00:04:28,466 --> 00:04:34,000 use the object inside the packaging solution class, which I'm going to do right now. 55 00:04:34,001 --> 00:04:37,606 I'm going to do packaging solution dot package 56 00:04:37,618 --> 00:04:41,000 and then I need to provide six coordinates. 57 00:04:41,001 --> 00:04:47,000 So let's put one two three four five and six. 58 00:04:47,001 --> 00:04:53,025 Okay, let's keep it simple and let's run that and you can see pick object on one to 59 00:04:53,037 --> 00:04:59,000 place object on three four pick object on three four and place object on five six. 60 00:04:59,001 --> 00:05:03,877 Okay, so this when I call package it's going to call the different 61 00:05:03,889 --> 00:05:09,000 methods from the robot right and the robot left we have created here. 62 00:05:09,001 --> 00:05:13,771 Okay, which is going to go here and then you 63 00:05:13,783 --> 00:05:19,000 can see print the pick object and place objects. 64 00:05:19,001 --> 00:05:20,000 Great. 65 00:05:20,001 --> 00:05:32,646 So now you have both used the inheritance here and the composition concepts with ROP and Python to quickly recap you can use inheritance when there is a relation 66 00:05:32,658 --> 00:05:45,000 where you can use the verb to be for example a robotic arm is a robot and you can use composition when there is a relation with the verb to have for example, 67 00:05:45,001 --> 00:05:51,000 the packaging solution has two robotic arms here we use composition. 68 00:05:51,001 --> 00:05:55,000 All right, that's the end of this bonus section on ROP with Python.