Now that we have the structure of the class, we have
the constructor and the attributes, let's
implement the methods of the class or in other
words, the functionalities of the class. And to
create a method, well, it's almost the same thing
as to create a function like you did previously.
And we have already actually our first method,
which is the constructor. Now we can add any
number of methods we want in the class, so still
with the indentation to stay inside of the class,
okay, and then def. And then I want to implement
the say_hi, method, okay. And as you can see, so
this is functionality of PyCharm. Whenever I
create a function or method inside a class, I
automatically get the self parameter. If you don't
get it, you simply write it, okay, so self, and we
don't want any other parameter. So as you can see,
the first parameter of any method would be self.
Okay, so we have the reference to the objects we
are currently using. So the method is really
linked, okay, to the object and to the class
Robot, it's not just a function, you can call from
anywhere. This is a specific function that is
directly linked to Robot, and you can only call
it in relation to the Robot class. Okay, so say_hi,
let's say we want to do so print. Hello, my
name is and then let's do self.name. So as you
can see, with the self keyword here, I can use
self.name. And because I have created self.name,
this is going to work here, okay, because I
have initialized the variable or the attributes,
okay. And then I can use it in any other method,
okay, and then plus, let's go back to a new line
here. Let's do ready to help. So that's the say_hi
method. And then I'm going to come back. So
come back to the same indentation here. And do def
init_hardware. So again, we just need self,
I'm not going to add any other parameter. Okay,
colon, actually, it's better if I end the print,
with the parenthesis here, and here, because this
is just an example, I'm going to do print init_hardware, okay,
just going to use a print key in
real life, you would actually implement this
method to init the real hardware, here we don't have
any hardware, just an example. So the point is to
understand our OOP, so let's use the print here. So
when we call that method, we can see that we have
actually called it and that it works. And then I'm
going to create another one def, let's say
print_info. Okay, again, we've self, I don't need
any other parameter. And what I'm going to do here
is I'm going to print all the info, okay, which is
the name, the version_number and internal_temperature.
So I'm going first to do self.say_hi,
okay. As you can see, I can directly call
the method of the class from another method of the
class, okay? And to do that, I simply do self.say_hi,
okay, if I just called say_hi, it's
not going to work because the function say_hi
doesn't exist globally. But the method say_hi,
exists inside the class Robot. So if I do self.,
I can get access to the different methods and
attributes here. Okay. So you can see how to call
the method from another method. And then let's say
print. So Version number: plus self.version_number,
I'm going to cast this as a string, because this
is probably going to be integer float, that can be
a string, but we don't know. Okay, the name here
is probably going to be a string, so I don't need
to cast it. But here let's take some precaution, and
then print temperature plus, and then the same self
internal_temperature. Okay, so in this method,
you can see we've got another method and we use
the different attributes. Okay, so self, and the
attributes. If you want, you can also modify the
attributes, okay. You just do self.version_number
is equal to and they just change it. Okay? That's
not the problem. So now we have a constructor, we
have three attributes, and we also have three
methods inside the class. All right, we have
successfully written all the functionalities of
the class inside different methods. And now let's
go to the next step.