You now have a class with attributes, a constructor
and methods, great. But this is just a structure
inside your program. If you run your code now like
this, well, nothing is going to happen. So
actually, let's do that. Let's do Run, and code, so
oop_example. Well, this works. But we have
nothing. Okay? If I add, let's say, an add here,
[No audio]
print, say, test, okay, with zero indentation, I
run it, and I see test. Okay, so test is executed
the class, well, nothing happened. So just as we
have seen with a function, well, a function is
just a bunch of code that you need to call in your
program, otherwise, it won't do anything. And this
is the same for classes, we need to create an
object or also called an instance of the class,
so we can use the class functionalities. So let's
actually do this, I'm going to create an object
here. And I'm going to call it my_robot is equal
to Robot like this. And I open the parenthesis, and
as you can see, so I have the suggestions from
PyCharm. So this, those here are actually the
parameters from the constructor here. So we need
to provide the name and the version_number. So
let's say our Robot is named R2D2. And the
version_number is, so here, well, for the version_number,
you could use an integer, float, string,
whatever. And I'm just going to keep it very
simple, and say this is the version two of the
Robot. So now I have my_robot. Okay, so to
create an object, you call the constructor simply
by as you can see, writing the name of the class,
and then parentheses. And if there are any
parameters here after self, you then need to
provide the parameters. So name, and version_number,
what's going to happen is that the name
here, this is going to be a local variable, that
is going to be assigned in the self.name, so in
the attribute of the class, and the same for
version_number, okay. So after we call this, this
is going to be self dot name, this is going to be
self.version_number in the class. And now what
I can do, so here, it's not going to do anything,
just create the Robot with the constructor. And
then what I can do, for example, is my_robot.say_hi,
okay, and then my_robot.init_hardware,
and then my_robot.print_info. Okay,
so as you can see here, so the self, the first
parameter is actually not something that you have
to add here, the self refers to the object. So if
you have this, it means you have to call the
method directly from the object. So my_robot.say_hi,
it's going simply to call this and pass
the my_robot object as the self here, as simple
as that. And then if you have any other parameter,
you call the parameter. But otherwise, that's it.
So I'm going to run the test. So I create an
object, and then I call the different functions or
the different methods. And then I can also get
access. So let's say print, I can get also access
to my_robot, for example, dot, let's say name. So
you can get access of the attributes, okay, of the
class, also directly with my_robot.name. Let's
run this now. And let's see what we have. So we
first create the Robot. And then you can see say_hi
hi is going to say Hello, my name is R2D2
ready to help. Actually, I'm just going to add these here.
And then Init hardware, okay, because we call it init_hardware method.
And then you can see we call print_info,
which is going to call say_hi, another time,
so hello again. And then Version number:,
Temperature, which is from the print_info method.
And then we'll print my_robot.name, which is
going to print R2D2, which is the
attribute of the class directly. Okay, nice. Now
we have an object created from the Robot class.
But actually, we can create as many objects as we
want from the class. Okay? Just as when you create
a function, you can just call it as many times as
you want, here, I'm going to create another Robot,
let's say my_robot2. Okay. And then my_robot2
is going to be Robot, also. So the constructor and
I'm going to give a different name. Let's say C3PO,
and this is going to be Version
one of the Robot. And I'm going to remove that
and remove that. And I'm simply going to do my
my_robot.print_info and my_robot2.print_info.
Okay, so I call the same method from
two different objects, which have two different
sets of attributes. Okay, I run this, and you can
see, Hello, my name is R2D2, Version
number: 2, Temperature 25. And then my name is C3PO,
and Version number: 1. Okay, so here
we have two complete independent and different
objects, okay, using the same class, the same
blueprint, but that have a different set of
attributes, okay, and you can use them
independently. So to recap, in order to create an
object from a class, you need to write the name of
the class first, to call the constructor and pass
the required parameters to initialize attributes.
Then from that object, you can call the class
methods. You can also create multiple objects from
the same class, and each object will have its own
internal mechanism and attributes independent from
the other objects.