Oops Concepts In Python

The ability to create apps using an Object-Oriented methodology is provided by Object-Oriented Programming, also referred to as OOPS concepts in Python. It accomplishes this by grouping together behaviours and properties that are similar or linked and converting them into objects.

Object-oriented programming uses the concept of “objects” to represent data and methods. With its own logic and data for internal communication, each individual object represents a different part of the application.

Python has always been an object-oriented language, just like many other popular general-purpose programming languages. With it, we can create programs with an Object-Oriented structure. It’s simple to make new objects and use them in Python programs.

To develop a program with the help of classes and objects is by using an object-oriented paradigm. There are literal connections between the object and things like books, houses, pencils, etc. The OOPS concepts in Python centers on creating code that can be used in multiple places. Creating new objects is a common solution to the issue. Let’s discuss more on the OOPS concepts in Python.

The following are the foundational pillars of any object-oriented programming language, which also includes the OOPS concepts in Python.

  • Class
  • Object
  • Inheritance
  • Polymorphism
  • Data Abstraction
  • Encapsulation


OOPs concepts in Python majorly deals with class. Simply put, a class is a group of objects. The objects are constructed according to the specifications defined in a class. It is a logical entity that consists of a few methods and attributes.

Let’s imagine, for the sake of illustration, that you needed to keep count of cats of varying breeds and ages, and so needed to create a class to do so. A list’s first item could indicate the cat’s breed, while the second could denote its age. Suppose there are a hundred distinct kinds of cats; how would you tell which part is which? Can you imagine if you wanted to give these cats special abilities? Exactly what is needed here are classes, because this is so disorganized. Learn more about Python by joining Python training in Chennai at SLA.

1.1Here are a few notes about the Python class:

  • Create a class by typing the class keyword.
  • All the information about a class can be found in its attributes.
  • It is always possible to access an attribute by using the dot (.) operator, as attributes are considered public. 
  • An illustration: Myclass.Myattribute

1.2Generating the empty class in Python

# Python3 program to

# demonstrate defining

# a class

class Cat:


In the above illustration, the class keyword was used to define a class with the name cat.


Objects are another important element of OOPS concepts in Python. The object at hand is a living, breathing thing with its own distinct personality and set of characteristics. A computer, keyboard, desk, table, pencil, etc. are all examples of real-world objects that could serve this purpose. Anything from integers to strings to floating-point numbers to arrays and dictionaries are all examples of objects.

More narrowly, a single string or number can be considered an object. The number 24, the text “Hello, world,” a list, which can contain additional objects, and so on are all examples of objects. You have probably been using things without realizing it.

2.1There are three parts to any object:

  • State: An object’s properties give as a representation of it. Furthermore, it directly reflects the qualities of the object being described.
  • Behavior: It is expressed through an object’s methods. This also shows how an object reacts when it encounters other objects.
  • Identity: It gives each object a distinct name and makes it possible for them to communicate with one another.

Let’s use the cat as an example of a class pet to think about its present state, typical behaviors, and unique identity. 

  • One way of thinking of this identity is as a cat’s name.
  • The cat’s age, breed, and fur color are all relevant characteristics to take into account when describing the animal’s state or attributes.
  • The cat’s behavior can be interpreted as indicative of either feeding or resting.

2.2Creation of Object

#creation of an object in Python




With this, an object with the Cat class definition will be created and assigned the name obj. Let’s get a handle on a few key terms before we delve deeply into objects and classes. OOPS concepts in Python are of prime importance. Get to know about Python by enrolling in SLA, the best Python training institute in Chennai.

3The Self

  • You can’t define a class method without including an additional initial parameter in the definition. When we invoke the method, we don’t specify a value for this parameter because Python supplies it automatically.
  • We still need one argument even if we have a method that doesn’t require any.
  • It’s the equivalent of a C++ pointer or a Java reference.
  • This is the sole purpose of the special self. When we invoke a method of this object as myobject.method(arg1, arg2), Python automatically converts it to MyClass.method(myobject, arg1, arg2).

3.1The __init__ method

Comparable to Java and C++ constructors, __init__ is an initialization procedure. It’s executed every time a class’s object is created. The method can be used for any object initialization purposes.

Therefore, let’s define a class and make some objects with the self and __init__ methods.

3.2Illustration : Creation of a class and object with class and instance attributes

class Cat :

# class attribute

attr1 = “mammal”

# Instance attribute

def __init__(self, name):

self.name = name

# Driver code

# Object instantiation

Lucky = Cat(“Lucky”)

Lucy = Cat(“Lucy”)

# Accessing class attributes

print(“Lucky is a {}”.format(Rodger.__class__.attr1))

print(“Lucy is also a {}”.format(Tommy.__class__.attr1))

# Accessing instance attributes

print(“My name is {}”.format(Lucky.name))

print(“My name is {}”.format(Lucy.name))


Lucky is a mammal

Lucy is also a mammal

My name is Lucky

My name is Lucy

3.3Illustration: Creation of a class and object with Methods

class Cat:

# class attribute

attr1 = “mammal”

# Instance attribute

def __init__(self, name):

self.name = name

def speak(self):

print(“My name is {}”.format(self.name))

# Driver code

# Object instantiation

Lucky = Cat(“Lucky”)

Lucy = Cat(“Lucy”)

# Accessing class methods




My name is Lucky

My name is Lucy


Inheritance, a key OOPS concepts in Python, is defined as the ability of one class to acquire the characteristics of another. The class that inherits properties from another is called the base class or parent class, and the class that inherits properties as a child class is called the derived class or child class. Among inheritance’s many advantages are:

  • It accurately depicts relationships in the real world.
  • Allows for the code to be used in several places. The same lines of code won’t need to be written over and over. It also lets us extend an existing class with new functionality without changing the original.
  • Inheritance is transitive, which implies that if one class, B, inherits from another, class A, then all subclasses of B also inherit from A.

4.1Inheritance Types:-

Let us now look into each type of Inheritance, a key OOPS concepts in Python.

Single Inheritance

Inheritance at the single level allows a derived class to take on the properties of a single-parent class.

Multi-level Inheritance

Using multi-level inheritance, a derived class can take on the characteristics of its parent class, which in turn can take on the characteristics of its parent class.

Hierarchical Inheritance

Several derived classes can share the same set of inherited properties from a single-parent class.

Multiple Inheritance

By utilizing several levels of inheritance, a single derived class can take on characteristics from multiple superclasses.

Inheritance is always considered as one of the important OOPS concepts in Python. Learning more about Python will help you easily understand the concepts related to it. Reach us at SLA for the best Python certification training in Chennai.

4.2Inheritance in Python

# Python program to show how parent constructors

# are called.

# parent class

class Person(object):

 # __init__ is known as the constructor

def __init__(self, name, idnumber):

self.name = name

self.idnumber = idnumber




def details(self):

print (“My name is {}”.format(self.name))

print(“IdNumber: {}”.format(self.idnumber))

# child class


def<__init__(self, name, idnumber, salary, post):

self.salary = salary

self.post = post

 # invoking the __init__ of the parent class

Person.__init__(self, name, idnumber)

def details(self):

print(“My name is {}”.format(self.name))

print(“IdNumber: {}”.format(self.idnumber))

print (“Post: {}”.format(self.post))

# creation of an object variable or an instance

a = Employee(‘Leela’, 125421, 400000, “Developer”)

# calling a function of the class Person using

# its instance






My name is Leela

IdNumber: 125421

Post: Developer

Two classes, Person (the parent class) and Employee (Child Class), are described in the preceding section. There is a direct line of descent from the Human class to the Employee class. As can be seen in the show function, we may access the features of the person class by way of the employee class. As can be seen in the case of the details() method, a child class can also alter the functionality of the parent class.


Polymorphism, another key OOPS concepts in Python, simply means having multiple forms. We can use polymorphism to do tasks that previously required multiple functions, such as determining whether or not a specific bird species is able to fly.

5.1Polymorphism in Python

class Bird:

def intro(self):

print (“There are many types of birds.”)

def flight(self):

print (“Most of the birds can fly but some cannot.”)

class sparrow(Bird):

def flight(self):

print(“Parrots can fly.”)

class ostrich(Bird):

def flight(self):

print (“Ostriches cannot fly.”)

obj_bird = Bird()

obj_spr = Parrot()

obj_ost = ostrich()








There are many types of birds.

Most of the birds can fly but some cannot.

There are many types of birds.

Parrots can fly.

There are many types of birds.

Ostriches cannot fly.


One of the cornerstones of OOPS concepts in Python is the idea of encapsulation. It explains the concept of enclosing data and the methods that operate on data in a single package. Limiting direct access to variables and methods can help avoid inadvertently changing data. For security reasons, a variable in an object cannot be modified in any other way than through the object’s method. They are referred to as “private variables” for obvious reasons.

Encapsulation can be seen in a class, which contains all of its data (member functions, variables, etc.) within its own boundaries.

Encapsulation in Python

# Python program to

# demonstrate private members

# Creating a Base class



self.a = “SLAJobs”

self.__c = “SLAJobs”

# Creating a derived class

class Derived(Base):

def __init__(self):

# Calling constructor of

# Base class


print(“Calling private member of base class: “)


# Driver code

obj1 = Base()


# Uncommenting print(obj1.c) will

# raise an AttributeError

# Uncommenting obj2 = Derived() will

# also raise an AtrributeError as

# private member of base class

# is called inside derived class



The c variable above has been declared with the private attribute. We have no way of knowing or changing the value of this characteristic.

7Data Abstraction

The user is protected from viewing the unwanted parts of the code by the use of Data abstraction, an OOPS concepts in Python. In addition, data abstraction was developed for circumstances in which it was undesirable to reveal private aspects of the code’s implementation. Python’s abstract classes are a powerful tool for implementing data abstraction.


The “private” access specifier is not available in Python, unlike in Java. For the most part, it works like an “object-oriented” language, supporting most of the typical features with the exception of strict encapsulation. As a result, it is not 100% Object oriented.

We have reached the end of our OOPS concepts in Python blog. I trust you now have a thorough understanding of Python’s class, object, and object-oriented notions. Take advantage of every opportunity to practice and master Python by joining Python training in Chennai at SLA.