And one last thing I want to show you about Python OOP is the concept of composition.
Let's say that now you have to work with a packaging solution.
This packaging solution is nothing more than two robotic
arms assembled together to work on an assembly line.
What you can do here is to create a class, I'm going
to create a new class here, class, packaging, solution.
And inside this class you're going to keep and
use two instances of the robotic arm class.
This is what we call composition.
Basically when you use an object from another class inside of a class.
So let's see with this example.
I'm going to create a constructor here, so def init, and I'm going to pass it.
So let's say we have a robotic arm on the right and a robotic arm on the left.
I'm going to name it robot right and robot left.
And do self.robot right is equal to robot right.
And then self.robot left is equal to robot left.
So I create two attributes of the class which are
going to be two objects from the robotic arm class.
And then I create a new method def, let's say package.
We are going to add some parameters.
So let's say pick x and then pick y and then place x and place y.
The coordinates we want to pick and place the object from and to.
And I'm going to also add middle x and middle y.
So I'm going to make the two robotic arm collaborates between each other.
So what I can do, for example, I can do self.robot right.pick object with pick x.
And pick y.
And then self.robot right.place object with, let's say middle x and middle y.
And then self.robot left this time, which is going
to pick the object from the middle x and middle y.
And then self.robot left.place object to place x and place y.
So basically what I do is I make the robot right,
pick the object and place it in the middle position.
And then the robot left is going to pick the object from
that middle position and place it in the place position.
Okay, so that's just for the example.
So you can see that we use the instances of that class inside that class.
So now this is ready.
What I can do is go back here and I'm going to do from robot import packaging solution.
And I'm going to add a new robotic arm.
So let's say robotic left.
I'm going to call it left in this one robotic right.
Okay, this is going to be robotic arm.
Let's call it.
So Bob and John.
Version two rage.
Let's put 300 also.
Then I'm going to remove that.
I'm going to comment that.
And what I can do now is I create a packaging solution.
So packaging solution is equal to packaging solution.
Okay, and I need to give two parameters which are this time two objects.
So robotic arm right first robotic arm right and robotic arm left right.
And so now I have two objects here, but I also can
use the object inside the packaging solution class, which I'm going to do right now.
I'm going to do packaging solution dot package
and then I need to provide six coordinates.
So let's put one two three four five and six.
Okay, let's keep it simple and let's run that and you can see pick object on one to
place object on three four pick object on three four and place object on five six.
Okay, so this when I call package it's going to call the different
methods from the robot right and the robot left we have created here.
Okay, which is going to go here and then you
can see print the pick object and place objects.
Great.
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
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,
the packaging solution has two robotic arms here we use composition.
All right, that's the end of this bonus section on ROP with Python.