This video demonstrates how to create classes in Python. A class is nothing but your own custom data type that allows you to create your own objects that are based on the classes you create. So, it is safe to say that a class is somewhat like a template. For example, if you create a class known as “person”. This class may include say, the first and last names of the person and a few comments about the person. Each of these attributes define an object within the class and so you can create a large number of objects within the class.
To create a class called as ‘person’, all you need to do is enter ‘class Person’. Take a note of the naming convention used here. The name of the class is generally begun with a capital. This is not a rule and so, it is not necessary that you have to follow it but, it is a convention that makes it easy to read your code.
A class always needs the support of a function known as the constructor. This function begins with a double underscore- init and ends with a double underscore. The constructor also needs to have at least one argument within it. Once again the default naming convention used here is ‘self’. A function always needs to begin with def. So, if you were creating a constructor for the class you just created, it would look something like this. “def__init__(self, fname= “”, lname= “”, comments= “”).
The attributes within the constructor also need to have a default variable assigned to them and in this case, as you can see above, the default variable is an empty string. The next step after creating the constructor is to make some initializations. The argument self in the constructor refers to the object itself that is being created. So, you need to initialize this accordingly. You can do this by entering the following.
self.fname = fname
self.lname = lname
self.comments = comments
You could have a different variable when initializing. For example, self.fname could also be self.firstname. However the local variable needs to remain same as that declared within the constructor. So it would be self.firstname = fname and so on.
Adding functions
You can then go ahead and add more functions to the class that you have just created in the same way that you created the constructor. For example, you can use the __str__ function to add to the class. This is also very much like the __init__ function and has to be defines in the same manner. Here is how you can do it.
def__str__(self): This is the minimum requirement to create the function. You could go ahead and add some more attributes to this function if you want. For example, let us use the following in the class that you created using the directions mentioned above.
def__str__(self:
return self.fname + “ “ + self.lname + “ , comments =” + self.comments
If this code runs a bit too long, you can continue to write it on the next line by simply adding a backslash (\) to the point before which you would be shifting the code to the next line. This completes the creation of your class. You now have a class known as person with a constructor and additional function within the class. The second function here actually controls the way you will be seeing your output.
Adding objects to the class
Your next objective after having created a class would be to add objects to the class. To do this, you can simple assign the attributes of the class to any variable you like. For example, consider the following.
p1 = Person(“John”, “Doe”, “This is a Person”)
p1 here refers to a variable and you are calling the class “Person” and assigning the fname, lname and comments attributes of the class to the variable within the parenthesis. Once you have done this, you can print out the object by saying, “print (p1)” This will give you a clean output as follows.
John Doe, comments=This is a Person
Now, if you want to change any or all of the attributes of the variable p1, all you would need to do is use dot notation. So for example, if you say, p1.fname= “Jane” and then if you enter print(p1). The previous fname from the class would now be replaced with Jane. You must note that only the fname will be changed here and nothing else. If you wish you could go ahead and change any or all of the attributes related to p1 in the same manner. However, at present the output should look somewhat like this.
Jane Doe, comments=This is a Person
You can also perform encapsulation on the object if you like. Encapsulation is nothing but hiding a part of the data from the output. So, you will be creating private variables that cannot be accessed from outside the class. The only way to access this data from outside the class would be by providing public setter and getter methods. This is something that is advanced and also quite exhaustive as a topic on its own. As a result, it is beyond the scope of discussion at this point of time in this video. However, that certainly is a subject for a another tutorial in the future.
To check out our Python courses which covers this, click here.
Please Share This Knowledge With Others!