What, why and how of the OOP in Python and why should you care?

What, why and how of the OOP in Python and why should you care?

Vipul kunwar's photo
Vipul kunwar
·Mar 20, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What is a OOP in Python?
  • Why use the OOP in Python?
  • How to use the OOP in Python?
  • Conclusion:

We all love objects whether balls, tables or anything. But do you know we can use the objects in Python? Python provides you with a way to handle the complex program using OOP.

So, what the hell OOP is?

OOP is object-oriented programming that deals with objects rather than logic.

For example,
A car is an object that holds various properties. Such as speed, colour and mileage.
So, let’s start the journey of the OOP in Python.

What is a OOP in Python?

Object-oriented programming is a way in Python to deal with the problem using objects.
The objects are bound together to perform a real-world solution.

Why use the OOP in Python?

You need to use the OOP in Python to solve a complex problem.
For example, you need to trace the breed, age and colour of the thousands of dogs. It’s hard to trace.
In that case, you can use Object-oriented programming in Python.

OOP will create many objects for the breed and the ages of the dog.
To trace every property of a dog it’s easy to do.
So, you can use OOP in Python to deal with a large set of problems.

How to use the OOP in Python?

You can use the OOP in the Python with help of its components.
Here are components of the OOP in Python:
• object
• class
• Encapsulation
• polymorphism
• Inheritance

1. object

Objects in Python using to create a variable or entity that holds different values.
For example,

class Myclass():     
    age = 23    

# Creating the object to call the age of the Person   
# P is an object     
P = Myclass()     



2. class

You can define the class using the class keyword. class is a blueprint of the objects. For example, a building has a blueprint that is the design of the building. In that design, you can build the building.
You can define the class using the class keyword.

class MyClass():

3. Encapsulation in Python

Encapsulation is a way in OOP to bundle unit the data in one unit. For example, when you’re creating a building you’re making it with bundling using the blueprint. That means the class is a single unit that binds the data together.
For example,

class employee:     
    def __init__(self, name):   
        # creating a variable that’ll hold the value.       
        self.name = name    

    def show(self):     
        print(“Name is”,self.name)      

# Creating a object of the class     
emp = employee(“Vipul”)      


Name is Vipul

In the above example, you can see that data can only access using the class.

The outside of the class you can’t access it.
That’s the use of encapsulation.

4. Polymorphism in Python OOP

Polymorphism means many form of the one function. That means using the one same function name in different classes.

class car():    
     def type(self):    

     def color(self):    

class Airways():   
     def type(self):   
       print("Air india")  

     def color(self):   

def func(obj): 

obj_car = car() 
obj_airways = Airways()   



Air India      

So, In the above example, you can see that I’ve called the different classes with the same function name.
In polymorphism, you can use the same function name with different classes.

5. Inheritance in the Python

You can use inheritance to inherit some properties from the base class.
Inheritance uses two classes. First-class is a Parent class and the second class is a child class.
Using inheritance you can inherit the properties of the parent into the child class.
So, what are the benefits of Python inheritance?

Python inheritance allows you to
• reuse the code
• access the different properties on the child class
• No need to define the same properties for the different class
• You don’t need to change the parent class for the new class

Let’s see the example of the Python inheritance.

# A Python program to prove inheritance    
# Base or Superclass. Note object in the bracket.  
# (Generally, the object is made the ancestor of all classes)   
# In Python 3.x "class Person" is    
# equivalent to "class Person(object)"     

class Person(object):   

# Constructor  

def __init__(self, name):      
    self.name = name      
    # To get the name       

def getName(self):       
    return self.name   
# Inherited or Subclass (Note Person in bracket)   

class Employee(Person):         
# Here we return true      
    def isEmployee(self):        
        return True       

# Driver code   
emp = Person("Geek1") # An Object of Person      

emp = Employee("Geek2") # An Object of Employee      
print(emp.getName(), emp.isEmployee())


Geek2 True

In the above example, you can see that the Employee class has taken the properties from the Person class.
I’ve also added the isemployee() function in the child class. That shows us that we don’t need to change the original class. You can reuse the python code.
At last, you’ve seen the basics of Object-oriented programming in Python.
In python, you need to create the objects and use them as a constructor.


Finally, you’ve seen the basics of the OOP in Python. You need to create the objects in the Python OOP. Using Python OOP you can handle real-world objects.
In this blog, you’ve seen the different aspects of Object-oriented programming .
Object-oriented programming contains the below parts:-
• object
• class
• Polymorphism
• Inheritance
• Encapsulation

Using these all parts in OOP you can handle the data in the real world.
So, what do you think about the OOP in python? Is there any way you want to contribute, drop below!
Do you know I’m writing micro python every day?
To get it you can follow me @codewith_Vipul

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this