OOPs in Python

OOPs in Python

In this article, I am going to discuss OOPs in Python with examples. Please read our previous article where we discussed Dictionary in Python with examples. As part of this article, we are going to discuss the following pointers which are related to OOPs in Python.

  1. Before OOPS
  2. Functional Oriented Programming
  3. Procedural Oriented Programming
  4. After OOPS
  5. Where does Python belong to?
  6. What are OOPS?
  7. Features of OOPS
  8. Class and Objects in Python
  9. Self variable in Python
  10. Can we create more than one object to a class?
Before OOPs

Before oops(object oriented programming), there were two approaches, procedure-oriented programming and functional oriented programming, being used.

What are these?

All the three approaches mentioned above are programming paradigms. A programming paradigm is nothing but the style of writing a program. We can also call them as methodologies which are used in writing a software program. Different languages follow different methodologies.

Functional Oriented Programming

Functional oriented programming is a program paradigm where programs are built around functions. Functions can be used as parameters, can be returned, can be used in other functions etc. Here functions are everything and they should always return the same output for a particular input. Erlang, Haskell, and Sakaila are the languages which are built on this paradigm.

Procedural Oriented Programming

POP languages are C, Pascal, Fortran etc. These languages used procedures and functions to perform the task. This kind of programming divides the program into subtasks composed of several procedures or functions or routines. This approach is called Procedure Oriented Approach.

After OOPs

OOPs is paradigm or methodology to implement software. OOPs languages are C++, Java, Dot Net etc. While developing any software, by using OOPs we need to implement it using classes and objects. This approach is called Object-Oriented Programming.

Where does Python belong to?

Python follows both functional oriented and object oriented programming approaches.

What is OOPS?

Object Oriented Programming language is a methodology which uses the concept of class and object to program. Let’s understand the concept of classes and objects with an example of a vehicle.

For example, let’s list down the properties a vehicle possess

  1. Name
  2. Number of wheels
  3. Colour
  4. Brand

Now, if we consider ‘vehicle’ as class, then the properties which we listed above will be attributes of that class. The types of vehicles available are

  1. Bike
  2. Car
  3. Truck
  4. Jeep

These types of vehicles will be objects to the class vehicle. All the properties of a car, like the name of the vehicle, number of wheels it has, its colour, its brand can be stored in the class ‘vehicle’. In order to access the information of the car we can create and object for the class and access it. Let’s say ‘car_obj’ is the object created for class ‘vehicle’ and assume the following data

  1. car_obj.name will be car
  2. car_obj.number_of_wheels will be four
  3. car_obj.colour will be blue
  4. car_obj.brand will be audi

Similarly, let’s create another object with name bike_obj

  1. bike_obj.name will be bike
  2. bike_obj.number_of_wheels will be two
  3. bike_obj.colour will be black
  4. bike_obj.brand will be honda

For both the objects, car_obj and bike_obj, the class is the same i.e vehicle, the attributes are the same, but the data is different. You can create any number of objects to the class and use the attributes to save the data.

We can say that class is a template or blueprint with which we can create objects. The objects of the same class contain different data but the same attributes.


Let’s understand the terms familiar in OOPS.

  1. Class
  2. Object
  3. Encapsulation
  4. Abstraction
  5. Inheritance
  6. Polymorphism
  7. Abstract class
Class in Python:

A class is a blueprint or template which contains attributes.It is a specification of properties and action of objects.

Defining a class:

Defining a class in Python

Documentation string represents the description of the class. It’s up to you to include this i.e its optional. It’s a good programming practice to include this.

After the class keyword comes the name of the class. After the name of the class we should give colon : symbol. class can contain

Constructors are used for initialization purpose and variables are used to represent the data. Variables can be of three types are as follows:

  1. instance variables
  2. static variables
  3. local variables

Methods are used to represent actions. Methods are again of three types are as follows:

  1. instance methods
  2. static methods
  3. class methods
Example: Define a class (demo1.py)
class Employee:
   def display(self):
       print("Hello my name is Shiksha")

In the above program ‘Employee’ represents a class which is defined by the developer. display is the method in the class. The program doesn’t give any output because we haven’t created any object to access the class members (attributes).

Self variable in Python:

In the demo1.py the display method has a parameter named ‘self’. Let’s talk about it:

  1. self is a default variable that refers to a current class object or the current instance(object) of a class.
  2. Generally, by using self, we can initialize the instance variables inside the constructor. (To be discussed in detail later).
  3. By using a self variable, we can access instance variables and instance methods.
  4. It can also be some other name rather than ‘self’, but the most commonly used and preferred is ‘self’.
  5. It contains the address of the current object being referred.
Objects in Python:

An object is an instance of a class. Using objects we can access class members, store data etc. They are like physical entities of class.

Syntax: name_of_the_object = NameOfTheClass()

Example: demo1.py contd..

class Employee:
   def display(self):
       print("Hello my name is Shiksha")
emp_obj = Employee()


Here, we created an object ‘emp_obj’ for the class ‘Employee’. With the created object, we called the display method which is printing something to the console. Therefore, after creating the object of a class, we can access the attributes available in that class with the created object as shown below.

  1. name_of_the_object.method()
  2. name_of_the_object.variable
Can we create more than one object to a class?

Yes, the main concept of OOPs is class acts as a template for the objects. So, any number of objects can be created for one class.

Example: Creating more than one object (demo2.py)

class Employee:

   def display(self):
       print("Hello my name is Rahul")

emp_obj1 = Employee()

emp_obj2 = Employee()


OOPs in Python

In the next article, I am going to discuss Constructors in Python. Here, in this article, I try to explain OOPs in Python. I hope you enjoy this OOPs in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

2 thoughts on “OOPs in Python”

  1. blank
    Abhishek Bittu

    Nice explanation. I till now…loved your course. The way you explained the things in detail is awesome.

Leave a Reply

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