Object Oriented Programming Concept in Python

Object-Oriented is a programming paradigm that follows the concept of classes and objects in place of functions and logic. It is also known as the fancy way of coding that organizes the code in a way that increases the code readability and maintainability. OOP concept is an important topic in programming and helps to build reusable modules for a variety of tasks in Data Science.

This is often a pre-requisite while building Deep learning models using various libraries such as Pytorch where the base model is reused to add custom layers. Let’s explore what this concept teaches and how to apply this in practical use cases.

What is the OOP concept?

Consider a smartphone that can be of any brand, but they’re a variety of common things among all of them. All have screens, speakers, buttons and on the software level, almost all of them are android powered. Now consider a case where every company is making their software from scratch, even the kernel which controls most of the hardware components.

This would become a tedious and expensive process, therefore, increasing the price of the devices. What if there is an abstract or generalized model that can be changed over time by any manufacturer according to their requirements? This concept tries to capture this class-based method where the code is structured in classes with different accessor methods.

Learn data science to gain edge over your competitors

Explore our Popular Data Science Online Certifications

What are Classes and Objects?

Classes are the blueprints of what has to be implemented. If we consider our previous example, we can have functionalities to call a person, receive calls, messages, play music, or do some other stuff.

All these things are common for every smartphone, their internal working is also similar and they can be considered as a class of smartphone functions or a class. Objects can be defined as all the smartphone brands that will use this common implementation in their products with modifications.

There can be multiple instances of this base class, and every instance can hold a different state of values without interfering with other objects. In Python,  a class can be declared by using the reserved keyword class. Further, __init__ constructor is used to initialize the class variables.

class Company:

def __init__ (self): = ‘upGrad’

def  display_name (self):

print(f”Company name is: {}”)

cm = Company()


Also read: Python Developer Salary in India

Top Data Science Skills You Should Learn

Different Pillars of OOP

Now that we are familiar with the basic building blocks of this paradigm, let’s look at some of the most important features/characteristics of this concept:


This states that methods (or functions) of the class and the data associated with it are encapsulated or protected from accidental or external access. This means that attributes that are defined in private or protected scope are not accessible outside the class.

There is a concern for Python that there is no concept of private variables in this language, so the attributes are accessible outside the class.

There is a way to recognize private attributes by using a double underscore at the beginning of the declaration and if you try to access this outside the class via the object of the same, you will be prompted with AttributeError because Python applies name mangling whenever it detects a private variable. This doesn’t give any security to your attributes because they are still accessible.


As the word suggests, it is taking a portion of an existing class called a parent class to a new class called a child class with little or no changes. We can connect this to our example in this way that all smartphone brands inherit a generic phone class that will help them perform basic functions, plus they can add their extra codes to enhance the user experience according to their needs. In Python, inheriting a class is done by:

class A:

some content

class B(A):

content of the derived class

There is another concept related to inheritance called function overriding. Suppose the camera function of the generic smartphone is not so good, and the manufacturer has a better solution for this. They can directly override this function by defining it again in the child class and apply the changes over there. 

Our learners also read: Learn Python Online for Free

Read our popular Data Science Articles


It defines the blueprint or an interface to the subclasses implementations. It means some methods are defined in the base class which is not fully implemented and only an abstract view is defined. It can help in tracking the various features of the module and sub-modules to be created.

For instance, some smartphones support NFC (near field connectivity) and this functionality can be defined in the base class and its implementation can be coded in the child class of the resultant phone. In this way, the abstract base class can provide the overall view of the module and subsequent implementations. Here is an example:

class Phone:

def camera(self):


def NFC(self):


class Xyz(Phone):

def NFC(self):

return True

Learn about: Top Python tools


If we go by the root meaning, it means multiple forms of the same thing. Polymorphism defines the functions based on the number or types of arguments passed. For instance, the length function in Python can take any type of iterable or object and returns the integer length.

This can also be quoted as function overloading but here is a catch in the Python language. We cannot define the same name functions with different arguments and if done, then it considers only the last entry. 

Practical Use Cases of OOP

We have seen what this concept is all about and what features it offers. Have a look at some examples where you can apply this concept:

Jinja Templating: If you have some experience with Python’s Flask framework which handles the routes and the server-side, this templating helps in handling this data on the front-end. Generally, a base HTML file is created which is then inherited by all the pages to have the same layout throughout the website. 

Kivy Applications: This is a library that allows you to build cross-platform (android and IOS) GUI-based python applications and here most of the programming is based on the OOP concept.

ORM: Object-relational Mappers offers a way to define the relational databases in application code using any language. For instance, in Django, you can define different types of models using classes for different types of users.

upGrad’s Exclusive Data Science Webinar for you –

ODE Thought Leadership Presentation



In this article, we discussed what is OOP concept, it’s building blocks (classes and objects), different pillars, and highlighted some examples where this paradigm is adopted. There are numerous places where this method of programming is considered due to better code management, collaboration, and providing abstract functionalities to other programs dependent on this.

If you are curious to learn about python, data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

What is OOP?

Object-oriented programming, abbreviated as OOP, is a computer programming technique that organizes software designs according to data, instead of organizing it according to functions and logic. OOP focuses on the objects, i.e., data fields with distinct properties and behavior that developers wish to control rather than the logic necessary to manage them. This programming method is ideal for large, complicated applications that are constantly updated or maintained. OOP covers manufacturing, design, and mobile application initiatives. Other advantages of this language are code reusability, scalability, and efficiency.

What are the different languages in OOP?

Object-oriented programming languages make up several of the most frequently used coding languages in today's computer industry. OOP languages are found everywhere. These languages make use of objects that hold both data and code. Encapsulation, abstraction, polymorphism, and inheritance are object-oriented programming ideas. Popular object-oriented programming languages include Java, Python, C++, Lisp, and Perl. They assist with programming utilizing the classes and objects paradigm. Other languages that support object-oriented principles are Perl, Objective-C, Dart, Lisp, JavaScript, and PHP.

Why are OOPs preferred?

There are many reasons why OOPs are preferred. Developers can reuse object-oriented code. You can also use inheritance to duplicate information and functionality that has previously been created. This saves time, simplifies the code, saves space, and makes coding easier on our fingers. Since most of the code is in one location and is called and reused, it is considerably easier to maintain. While most languages offer some level of security, object-oriented languages are more convenient since encapsulation includes security. Object-oriented programming languages divide a program into objects and classes. This is advantageous since it provides your application with a more modular framework.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks