HomeBlogData ScienceObject Oriented Programming Concept in Python

Object Oriented Programming Concept in Python

Read it in 7 Mins

Last updated:
1st Dec, 2020
Views
1,508
In this article
View All
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

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):

self.name = ‘upGrad’

def  display_name (self):

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

cm = Company()

cm.display_name()

Also read: Python Developer Salary in India

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:

Encapsulation

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.

Explore our Popular Data Science Online Certifications

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.

Inheritance

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

Top Data Science Skills You Should Learn

Abstraction

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):

pass

def NFC(self):

pass

class Xyz(Phone):

def NFC(self):

return True

Learn about: Top Python tools

Read our popular Data Science Articles

Polymorphism

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

 

Conclusion

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.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

1What 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.

2What 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.

3Why 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.

Suggested Blogs

Python Split Function: Overview of Split Function ()
1500
Introduction to the split() function in Python Split function in Python is a string manipulation tool that helps you to easily handle a big string in
Read More

by Rohit Sharma

25 May 2023

OLTP Vs OLAP: Decoding Top Differences Every Data Professional Must Know
1505
Several businesses use online data processing systems to boost the accuracy and efficiency of their processes. The data must be used before processing
Read More

by Rohit Sharma

12 Apr 2023

Amazon Data Scientist Salary in India 2023 – Freshers to Experienced
1500
Exploring Amazon Data Scientist Salary Trends in India: 2023 Data Science is not new; the International Association for Statistical Computing (IASC)
Read More

by Rohit Sharma

10 Apr 2023

Data warehouse architect: Overview, skills, salary, roles & more
1500
A data warehouse architect is responsible for designing and maintaining data management solutions that support a business or organisation. They analys
Read More

by Rohit Sharma

10 Apr 2023

Research Scientist Salary in India 2023 – Freshers to Experienced
1500
Salary Trends for Research Scientists in India: 2023 From pharmacology to meteorology, the role of a Research Scientist across diverse domains implie
Read More

by Rohit Sharma

10 Apr 2023

Understanding Abstraction: How Does Abstraction Work in Python?
1500
Python is one of the most extensively used programming languages. Python has made it simple for users to program more efficiently with the help of abs
Read More

by Rohit Sharma

08 Apr 2023

Understanding the Concept of Hierarchical Clustering in Data Analysis: Functions, Types & Steps
1503
Clustering refers to the grouping of similar data in groups or clusters in data analysis. These clusters help data analysts organise similar data poin
Read More

by Rohit Sharma

08 Apr 2023

Harnessing Data: An Introduction to Data Collection [Types, Methods, Steps & Challenges]
1504
Data opens up the doors to a world of knowledge and information. As the currency of the information revolution, it has played a transformational role
Read More

by Rohit Sharma

08 Apr 2023

Top 50 Excel Shortcuts That Will Transform the Way You Work In 2023
1500
Microsoft Office has become a compulsory tool in almost every modern workplace. According to research, 81% of companies use MS Office and some of its
Read More

by Rohit Sharma

06 Apr 2023