In this article, you will learn how to create classes and objects in Python.

Object-oriented programming (OOP) allows programmers to create there own objects that have attributes and methods making the code more reusable and organized at a larger scale. Code utilizing classes is generally easier to read, understand, and maintain. The two main concepts of OOP are classes and objects in Python:

Class: Class is basically a blueprint or a template for creating objects.

Object: Collection of arguments and methods which can be performed on those data. An object is nothing but an instance of the class.

Suppose you want to design a supercar, first you will create a blueprint that includes all its features and design, but you can’t ride it, after all, it’s just a blueprint, right? In order to ride the car, you have to build a real car based on that blueprint.

In Object-oriented programming, the class is like a blueprint that has characteristics of the objects, and a real car is an object of that blueprint, an infinite number of objects can be created from a single class. You might have already heard that “Everything is an object in Python”. Let’s verify that,

The type of the variable containing sting returns str class which means every string in Python is an object of the string class similarly any data is an object of Python’s built in classes. Beside these built-in classes, we can also create our own classes these are called user-defined classes.

Class In Python

A class functions as a template/blueprint that defines the basic characteristics of a particular object. A class is made up of attributes (data) and methods (functions). A very basic class in Python would look like,

A class in Python starts with the class keyword followed with a single space and the class name which by convention should start with a capital letter. Next, we have a pair of parentheses and colon(:) at the end.

Note that, everything inside the class should be intended because Python relies on indentation to know which block of code belongs to the class. Inside this class, we have two methods.

Objects In Python

An Object is an instance of a class, to create an object of a class in Python first we need to initialize the object as a class instance by setting it equal to class_name()

After object initialization, we can use the methods of the class using the dot operator ( . ) which acts as a reference for attributes of the class. Since the attribute which we are calling here is a function rather say method we need to add parenthesis ( ) in the end just like you would do for a regular function call.

Output: First method will print this

The Constructor Method

Classes have a special method called __init__ (for initialization) or Constructor method which runs whenever an object is initialized. The constructor method is always the first method of any class.  The general syntax for defining a class with the constructor in Python,

Let’s go back to My_class where we created a basic class and object, this time we will have a constructor in the class definition.

Output:

Notice how the constructor method printed out the string before the actual method? This is how constructors work in Python we do not need to explicitly call it therefore, it is generally used to initialize mutual attributes for the objects of the class.

Output: 12.56

In the above class, we have declared a class object attribute assigning the value of pi, now every method inside the class has access to pi.

This article went through creating, instantiating, initializing attributes with the constructor method of Classes and Objects in Python, hope you learned something new out of it. Have questions? Feel free to ask in the comment section below.

How To Construct Classes and Objects In Python

The article was published on October 12, 2020 @ 2:22 PM

Leave a Comment