Python Object Oriented Programming


Python is a multi-paradigm programming language. It means that Python supports different programming approach.

Python Object Oriented Programming approach (OOPs) is the  easiest and the most popular one among them .

Introduction to OOPs

Object Oriented Programming is referred as a programming approach , where the programs are organised as objects. This is to say that everything written in the program is treated as an object.

OOP is based on these four principles:

    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
Image result for oops in python
OOPs Python

Python Object Oriented Programming


Python follows the object Oriented programming style.

It treats everything inside the code, as an object.

An object has two characteristics :Attribute and Behavior

For example, let’s consider human as an object. A human has both attribute and behavior.

Name, age, color, sex etc are the attributes of human object, while walking, talking,eating, etc are the behaviors of human.

Therefore, any object created in Python, has attributes( defined in constructor) and behaviour (defined in methods).


Class is defined as a template or blueprint for an object.

Object is the instance of a class.

For instance, if we want to construct a house, we require a blueprint of the house. Similarly, Class is the blueprint required to build an object.

Therefore, a class has to be created before creating an object.

The syntax for creating a class is as follows:

class <class-name>:

For example:

class Mobile:

The syntax for creating an object is as follows:


For example:

m1=Mobile( )

To access the object created for a class, we use the dot(.) operator. For example: )

m1.purchase( )


Methods are the functions that are defined inside the body of the class.

They define the behavior of an object.

The syntax is as follows:

def <method-name>(self,<attributes>):

For example:

def buy(self, a,b)

self is the keyword that  points to that particular object ,it is referring to . In other words, self keyword points to the current object in use .

To clarify, for instance, there are multiple objects for the same class Mobile. Then, the object in use is present in self.

It creates a reference to the current object.

self behaves like this operator in Java and C++



Constructor is a special method in Python, that is declared inside a class.

The constructor is the first method that is called inside a class ,whenever an object is created for a particular class,

The syntax is as follows:

def__init__(self, <attributes>):

 Here, init is  the constructor.

Attributes are created inside a Constructor.

There are two types of constructors: default and parameterised constructor.

Default constructor does not accept any attributes, except self , which is inevitable.

def __ init__(self):


Parameterised constructor accepts some parameters/attributes from the programmer.

def __init__(self, <attribute1>,<attribute2>,..)


The above picture shows a code snippet of class Mobile,parameterised constructor with price and brand as attributes and object of class Mobile.The output of the code is as shown below:


Abstraction refers to hiding the background details from the user and only showing the necessary details.

The main use of abstraction is to reduce the complexity and increase the efficiency of the program.


Encapsulation is the method of restricting access to a particular data , inside the code .

Other programming languages like Java and C++, uses using access specifiers like private, public and protected., to protect its data. But Python DOES NOT support these keywords.

A private attribute is the one, that is denies access from other objects in Python.

It is represented by using __ before the name.For example, __sample, self.__acc, etc.

Private attributes are not  accessible  directly outside the class. To access it outside the class, we’ve to use getter and setter methods.

setter method  sets value to a private attribute.It always accepts parameters

def set_value(self,<parameters>)

getter method  gets the  value of a private attribute.

def get_value(self,<parameters>)



Inheritance is the process of inheriting the properties of one class, into another class .

In other words, the sub class acquires all the properties of the superclass.


Suppose we create a class A and we want to inherit all the attributes and behaviour from A into B, the we write   class B(A).

The inheriting class is the subclass(B) and the inherited class is  the superclass(A).Therefore, all the methods and constructor of class A is inherited to class B.


CODE REUSE is the biggest advantage of Inheritance.

There are four types of inheritance :

  • Single level inheritance : One parent class and one child class

Inheritance - python class - edureka

  • Multilevel inheritance : One grand parent class, one parent class and one child class.

Multilevel Inheritance

  • Hierarchical inheritance: One super class and two or more sub classes
  • Image result for hierarchical inheritance
  • Multiple Inheritance : Multiple parent class, but one child class

Image result for multiple inheritance

Please refer to the above link for program’s on inheritance.


Polymorphism is the ability to use common interface for multiple forms.

Python supports Method overriding, nut it does not support Method Overloading.

The code above ,shows a class Phone, inheriting  a superclass Mobile.

Both the classes have the method buy() in them . But the action performed by the buy( ) method in sub class, overrides  the super class’s buy ( ) method and it will print ” Bought a phone”. This illustrates Polymorphism in Python.

These topics cover the basics of Object Oriented Programming in Python.

I hope this article was of some use to you .

Thank you!


Leave a Comment

%d bloggers like this: