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, create a new class here, class PackagingSolution,
and inside this class, you're going to
keep and use two instances of the RoboticArm
class. This is what we call composition.
Basically, when you use an object, okay, from
another class, inside of a class, okay, 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 the robotic arm on
the right, and the robotic arm on the left. I'm
going to name it robot_right, and robot_left,
and 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
RoboticArm class. And then I create a new method
def, let's say, package, okay. We are going to add
some parameters. So let's say pick_x and then
pick_y, and place_x, and place_y, ok the co-ordinates. Next,
we want to pick and place the object from and to.
And I'm going to also add middle_x, and middle_y.
Okay, so I'm going to make the two robotic arm
collaborates between each other. Okay. So what I
can do, for example, here is do self.robot_right.pick_objects,
Okay, we've pick_x, and pick_y,
and then sel.robot_right.place_objects,
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_objects
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 PackagingSolution,
and I'm going to add a new robotic arm.
So let's say robotic_arm_left, I'm going to call it
left, and this one robotic_right. Okay, this is
going to be RoboticArm, let's call it so Bob and
John, version 2, reach 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 PackagingSolution.
So packaging_solution is equal to
PackagingSolution, 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 PackagingSolution 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 1, 2, 3, 4, 5,
and 6. Okay, let's keep it simple, and let's run
that. And you can see Pick object on (1,2), place
object on (3,4), Pick object on (3,4), and
Place object on (5,6). Okay, so this, when you
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_object. Great. So now you have both
used the inheritance here, and the composition
concepts with OOP 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
RoboticArm is the Robot, and you can use
composition when there is a relation with the verb
to have. For example, the PackagingSolution has
two robotic arms, can be used composition. Alright,
that's the end of this bonus section on our OOP with Python.