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.