A Complete Guide on OOPs Concepts in Python

Object-Oriented Programming or OOP has been an integral part of the software world for a while now. It is an excellent programming paradigm that offers a certain level of freedom and enhances the programming accomplishments for the developer. 

There are certain basic concepts and principles that every developer should know. They drive the premise for developing an application using this model.

Here, we are going to take a look at some of the crucial yet basic OOPs concepts in Python. These concepts drive the programmers towards achieving better results as well as to model their apps in an enriching fashion.

What Is Object-Oriented Programming?

For the uninitiated, this is where we are going to begin our induction. Let’s understand OOPs as a layman would, to get a deeper understanding of how we can work it out. 

In this programming structure, you can easily combine things with similar properties or behaviors into a single object. If you are talking about demographics, then that becomes a single object for this programming model. Similarly, if you are talking about actions to be taken, then that becomes another object.

They are all objects that contain raw data. Your software solution will program these objects in a way that they fulfill the end goal together. These objects will perform a few actions together, as programmed by you, and deliver the result.

An excellent example to help understand this would be an email program. 

Let’s say you have one object that contains the email contents, such as the recipient’s name and the subject. There is a second object that details the attachment and sending the email. You will design a program that will automatically combine these objects, ensure that a fully-written email is ready with the desired attachments, and sent it to the recipient. 

This is how OOP works. However, some concepts drive this into action. Let’s take a quick look at these concepts and how they function.

Read: Is Python an Object-Oriented Language?

How to Define a Class?

The class is what defines every object, and is an important aspect of OOPs concepts in Python. Let’s say you have four objects, such as eyes, ears, mouth, and nose. These are parts of a face, which is the class. 

Let’s get started with how to define a class.

Let’s say you are talking about a class called email.

Class Email:


This is how the class is defined. Use the “pass” so that when you run the code in a compiler, there are no bugs issued. 

Only defining the class may not help you much. You need to add some properties to make it attractive and the code helpful. So, you need to add objects to your class.

The ._init_() method is what would prove to be useful when defining the properties within a class.

This will ensure that every single time you create a new object for the class, this method will set the parameters to the initial state, thus, initializing a new object every single time. 

The first parameter for this method would always be self, even when you assign multiple parameters to it. 

So, what happens in this case? Let’s say you created a new class. The instance is transferred to the self parameter. As a result, the ._init_() can assign fresh properties to the defined object. 

If you are still pondering on how to use it, let’s understand it via an example.

Class Email:

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Let’s break this code down for better understanding. 

The indentation expressed in the code above is critical. You should match the same when writing your program using OOPs in Python. Let’s understand the self variable in the above code.

The produces an attribute that is called name. The value of the name parameter is assigned to this attribute. Similarly, attributes and values are assigned for the other-self variables too.

These attributes are known as instance. You will need to specify the value of every attribute mentioned within a particular instance. Let’s say that there are two types of emails – welcome and nurture. The email recipient would differ for both instances (welcome emails and nurture emails).

Class attributes, on the other hand, are different; they will contain the same value for all class instances. For instance, these are all inbound emails. This is the class attribute that you can define for them.

Class Email:

                  #Class Attribute  

                  Email: “Inbound Emails”

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Learn about: How to Code, Compile and Run Java Projects

Understanding Class and Instance Variables

Let’s take the above example where we have created a class attribute as well as two instance variables to understand OOPs in Python better. 

Class Email:           

                  Email: “Inbound Emails”

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Let’s talk about instantiating the objects within this class. You need to set values for each of these objects. This will be the initial value for every object, as discussed in the earlier part of the guide. In case you don’t assign values to these objects, then you will receive a TypeError. 

>>> Email()

Traceback (most recent call last):

     File “<pyshell#6>”, line 1, in <module>

          Email ()

TypeError: __init__() missing 4 positional arguments: ‘name’, ‘recipient’, and ‘address’

We can add value immediately after the class name.

Nurture Email = Email (Nurture, David,

Welcome Email = Email (Welcome, Daisy,

With the above code, we have generated two instances, for a nurture email and a welcome email. Let’s check the __init__() defined after the class instance for the above code. We see four parameters, including self. However, no variable has been mentioned for self. Let’s understand why?

So, whenever you create a new object within a class, you are instantiating the class. You will be assigning a memory address while doing this, where the variable will be stored. As soon as you instantiate a new object, Python will automatically create an instance, and it will be passed to the first variable in the .__init__() method. As a result, the self is removed in this method. 

With the class variable, you can ensure that every instance you have created has a variable or value associated with it. This will ensure quicker and easier results, and thus, safer conclusions for your programming. 

Also Read: Must Read 47 OOPS Interview Questions & Answers For Freshers & Experienced

Summing Up

We have understood the various aspects of creating a class, object, and method using the Python OOPs programming model here. Apart from this, we have also understood concepts such as instances and instantiating, which are core to OOPs programming in Python. Next, you will need to understand the principles that govern these instances and classes such as inheritance, polymorphism, abstraction, and encapsulation. They are core OOPs concepts in Python that drive your application and its results.

To move further in the Python funnel, you need to have your basics clear and rooted. So, dive into classes and understand how it works. Play around a bit with these variables, and understand their outcomes before you can set-up your application. 

If you are curious about learning data science to be in the front of fast-paced technological advancements, check out upGrad & IIIT-B’s PG Diploma in Data Science and upskill yourself for the future.

Become a Full Stack Developer

Enroll Today

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Data Science Course