My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Classes in python

Classes in python

Victory Chiamaka Wekwa
·Jun 15, 2020

In this article I will be writing about an important concept I just learnt about in my journey to becoming a software developer called classes. To a newbie hearing the word for the first time, the person would be wondering what has classes got to do with programming. Not to worry that was exactly my thoughts the first time I came across classes. I will be explaining the concept of classes and leading you to create your first class in Python. Sounds good right? So let's go. Classes when defined represent a whole behavior in which an application can have. It can be used to represent real life situations as applications can be made from classes that have already been defined.

Concepts in classes

Objects: Objects when created created in a class take part of the behavior of the class defined and objects in classes are called instances. The properties of an instance in a class is called instance variables while the name are called instance methods.

Methods: Functions can also be defined in a class but then when defined in a class they are called methods. Every method in a class always have a self parameter. In classes, there is an important method known as the constructor method. In python, it is denoted as init. The two underscores before and after the init are very important as it enables your constructor method to take the behavior of the class defined and be callable and useable. That's it with the theory, let's move on to create our first class.

Creating a class

So how are classes created?

In creating a class, the class statement is used, then your class name followed by a colon as seen below.

IMG_20200614_153011_562.jpg

It is important to note that when defining a class, the class name should begin with a capital letter. After defining the class, the next step is to define the constructor method indented below the class name as seen below.

IMG_20200614_153034_189.jpg

The constructor method should also have other attributes in which you want your class to have as a parameter after the self parameter.

As seen above, in the class Restaurant we have parameter like the restaurant name and the cuisine type. The body of the constructor should look as above because each method in a class definition begins with a reference to the instance object. The self parameter is used to create member variable inside a class. The code above created member variable for the object created by the constructor method. It is important to note that member variables begin with self to show that they are member variables. By now you should be wondering , what about the instance method and how they created. Well, here is it. How to create an instance from a class. Instances can be created from a class by calling the class name and passing whatever argument the init method accepts. For our Restaurant class, our init method accepts the restaurant name and the cuisine type. In creating our instance should look as below.

IMG_20200615_100221_200.jpg

And calling our instance created in a class should look as below.

IMG_20200615_100150_982.jpg

Different method can can also be defined in our class to give more meaning to our class. As seen below.

IMG_20200615_105254_371.jpg

Finally, as a software developer writing classes will make your project easier to read and understand. It will help you organize information about a data or an application you are building so that elements can be reused when making multiple instances. Hope you enjoyed this, I will love to hear your thoughts, kindly leave a comment or like this article.

Still want to learn more about classes. Check here.https://brilliant.org/wiki/classes-oop