The first thing to do when creating a class is to
actually define what attributes and what methods
you need to put into the class. Well, that's what
we have just done in the previous lesson. So now
we can start the implementation with Python code.
And let's write our first class, Robot. So I have
created a new file here, doesn't really matter for
the name, so for example, and to create a class it
is very simple, you use the class keyword, and
then the name of the class and then colon. So
class keyword name of the class, colon, and for
the name of the class, we are going to use a
different convention than before. Here, you are
going to use an uppercase letter, okay, for each
new word, okay, and if you have different word,
for example, RoboticArm that we're going to see
later, well, you're going to put an uppercase for
each new word. Alright, that's the convention.
And then you press enter, and you can see that we
have a new indentation of four spaces here. So
you're going to write your class directly here
with the indentation, okay, so everything that is
indented is inside the class, everything that is
not indented after that is outside of the class.
And well, the first thing we have to do in this
Python class is to create a constructor. The
constructor is a method. Okay, so that's a
function for a class. So this is a method, which
will be called first when you create an object
from the class. Because remember, a class is just
a structure, it does nothing by itself. You will
need to create some objects, so you can use the
functionalities of the class. And when you create
an object from the class, the first thing that is
called is the constructor. And we're going to see
just a little later how to create an object, now
let's focus on the constructor. So the constructor
will always be the same def. So you create def,
like you just create a function, and then
__init__,
and then you open parentheses, okay, I
have the auto completion here, the first
parameter, you are going to give his self. Okay, I'm
going to explain self just a bit later. And then
you can add different parameters. So basically,
the constructor will be called first when you
create an object. So that can be a good point to
initialize your attributes. And so you're going to
pass the attributes of the class in the
constructor parameters. So for example, we have
name, version_number that we want to give to the
Robot. Okay, I'm going to press Enter here. And
of course, we have a new indentation for this
function. So the function, so the method here is
indented inside the class Robot, and then we have
yet another indentation here. And what I'm going to
do, I'm going to initialize the attributes by
using self.name is equal to name, and then
self.version_number is equal to version_number.
And then I'm going to add self.internal_temperature
is equal to and let's put just an
arbitrary number, let's say 25, as a float. So
here in the constructor of the class, we
initialize the attributes of the class, okay, and
to initialize the attributes, we actually to
create some new variables with self, okay. So, for
each method of the class, you are going to have
first the self parameter. So when you use self,
this actually refers to the object, okay, related
to class Robot. And well, how to make the
difference between a simple variable and an
attribute of the class? Well for an attribute of the
class you're simply going to use that self and to
self., and then for example, name. So here, the
name is the parameter. So this is a local variable
inside this method, okay, if you remember from the
scope. self.name, is actually in the class.
So if I use another method, I'm going to see that
just later, I'm going to be able to retrieve the
self.name because this is inside the class of
inside the object Robot that we have created with
the constructor here. Alright, so to recap, you
create a class with the class keyword and then the
name colon, and then you're going to use different
functions are listed here, indented inside the
class. And the first internal method is the
constructor, which is the init method. All right,
we have now created a class structure for the
Robot, and we have successfully created the
attributes inside the class constructor.