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, for example. Well, this works but we have nothing.
If I add, let's say I add here print, let's say test, with zero indentation,
I run it and I see tests. So the test is
executed but 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,
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 myRobot
is equal to robot, like this, and I open the parentheses. And as you can see,
so I have the suggestions from PyCharm, so
these, those here are actually the parameters
from the constructor here. So I need to provide a name and a 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 2 of the robot.
So now I have my robot. 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.
So after we call this, this is going to be self.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.sayI,
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.sayHi, 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. I'm going to remove 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. And 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 sayHi is going to say hello, my name is
R2D2 ready to help. Actually, I'm just going
to add this here. And then init hardware, okay,
because we call init hardware method. And then
you can see we call print_info, which is going to
call sayHi another time, so hello again. And then
version number temperature, which is from the
print_info method. And then, well, 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 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 two, temperature 25. And then
my name is C3PO and version number one. 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.