Summary: In this tutorial, we will learn what is object oriented programming in Python, what are class and objects, and how can we create a class and objects in Python.

What Is Object-Oriented Programming?

Object-oriented programming is a programming paradigm based on the concept of “objects”, which can contain data as properties and behaviors as functions.

Object-oriented programming is a way to bundle related properties and behaviors into objects. It allows emulating the real or non-real entity into programming.

The entity can be any object such as a car, dog, person, etc that has properties and behaviors.

For instance, consider a car, it is a real-world object that has properties like color, engine, width, etc. and behavu=iors like driving, blowing horns, etc.

In OOP, by using class and objects we can easily represent such entities with properties and behaviors as objects in programming.

Let’s see what are class and objects in Python.

What are Class and Objects in OOP?

The class is a blueprint for creating objects. It is an empty structure that defines what properties and behavior its objects will have.

whereas an object is the instance of a class. It is created by initializing the class properties with values specific to the object.

For instance consider students going to a school, each student of the school has his/her own roll no, name, and date of birth, etc (also known as attributes or properties).

Although everyone in a school is a student, they are different from each other.

Similarly, in context to OOP programming, the student is a class, that defines others but doesn’t contain anything whereas every individual person going to school has unique data (roll no, name, etc) is an object which is defined from the student class.

Let’s make this more clear by implementing it in python.

How to Create a Class and Objects in Python?

In Python, we define a class using the class keyword.

class Student:
    pass

A class doesn’t contain any data but has to define properties that its objects will have. So we define the properties in the __init__ method using the self parameter.

class Student:
    def __init__(self, rollno, name, dob):
        self.rollno = rollno
        self.name = name
        self.dob = dob

In Python, the functions are defined using the def keyword followed by the function name.

The __init__ method is a reserved method that is called when we create an object of the class. It is called only once and is used to initialize the properties of the class.

The method which we define inside the class without any decorator becomes the instance method. Python interpreter during the function call, auto passes the reference of the object as the first parameter. So, instance methods have to have self as the first parameter.

Apart from properties, the objects can also have behaviors (functions). So we define them using the def keyword inside the class.

class Student:
    def __init__(self):
        self.rollno = 7
        self.name = "Adarsh Kumar"
        self.dob = "20/01/2001"
        
    def attendance(self):
        print(f'{self.rollno} is marked Present.')
        
    def study(self):
        print(f'{self.name} is studying.')

The class (blueprint) is ready. Now we have to create its objects.

In Python, we can create an object by typing the name of the class followed by the parenthesis.

Student()

We can store the reference of the objects into a variable and can use the same to call any instance method.

stud1 = Student()
stud1.attendance()  #outputs 7 is marked Present.

Initialize the Object’s Properties by Passing Arguments

The blueprint which we have defined above has the same property value for every object. Every object that we create will have the same name, rollno and dob.

stud1 = Student()
stud2 = Student()

print(stud1.name)    #outputs 'Adarsh Kumar'
print(stud2.name)    #outputs 'Adarsh Kumar'

To assign the properties of objects with values specific to it, we will have to pass the arguments to the __init__ method.

class Student:
    def __init__(self, rollno, name, dob):
        self.rollno = rollno
        self.name = name
        self.dob = dob

In Python, the __init__ method is invoked during the instantiation of an object.

So to pass the arguments to the __init__ method, we have to provide values during the instantiation of the object (i.e. within the parenthesis).

stud1 = Student(1, "Simson", "20/01/2001")
stud2 = Student(2, "Jade", "15/12/2000")

print(stud1.rollno)    #outputs '1'
print(stud2.rollno)    #outputs '2'

print(stud1.name)      #outputs 'Simson'
print(stud2.name)      #outputs 'Jade'

print(stud1.dob)       #outputs '20/01/2001'
print(stud2.dob)       #outputs '15/12/2000'

It is important to know that value for the self parameter in every instance method is the reference of the current working instance (object). It is passed by the Python interpreter, so we don’t have to worry about it.

What are the advantages of OOP in Python?

Primitive data structures such as string, number, list, tuples, etc. can store only simple types of data.

It would be difficult to manage complex type of data such as details of students using the primitives data types.

Consider the following code for instance:

stud1 = [1, "Simson", "20/01/2001"]
stud2 = [2, "Jade", "15/12/2000"]

Though we have successfully stored the details of the students in a list, it would be difficult to remember the index of the properties.

One will have to always remember that the roll number is at index 0 (stud1[0]), the name is at index 1 (stud1[1]) and so on.

Moreover, it may lead to serious ambiguity, if the list grows or any of the student’s details don’t match the correct order.

stud1 = [1, "Simson", "20/01/2001"]
stud2 = [2, "Jade", "15/12/2000"]
stud3 = ["Adarsh", 3, "02/12/2001"]

In such cases, we should use OOP i.e. define a user defined type of the student as class and create objects of the same.

Using class and objects makes the code more modular and simple.

In addition, it allows the code to use the following features of OOP to make it more secure and scalable:

Leave a Reply

2 × four =