Table of contents
1.
Introduction
2.
Class
3.
Object
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Objects and Classes in Python

Author Malay Gain
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In object-oriented programming, the concept of class and object is very essential. In Python, we also find such object-oriented characteristics. A class is a description of a set of objects that share the same properties (attributes), behaviour (operations), kind of relationships, and semantics. An object is an instance of a class. Here we will be discussing different properties of a class and object as well as how a class is defined, and an object is created.

Also see, Python Round Function.

Recommended topics, Floor Division in Python, and Convert String to List Python

Class

A class can be considered a user-defined blueprint from which objects are created. Classes are used to bundle data and functionalities together so that their objects can be created or classified under different classes based on their functionalities. Objects of each class can have their attributes to maintain the state, and their state values can be modified using the member functions.

Let’s understand this concept through an example.

If we consider student as a class, the student class has nameageenrollment_id as its attributes, and studyeatsleep as its behaviour.

Defining class

#a class to denote student of CSE department
class Student:
  
  #calss attribute
  department="CSE"

  #constructor in Python
  def __init__(self,name,enr_id):
    #instance variable
    self.name=name
    self.enr_id=enr_id

  def get_name(self):
    return self.name
  
  def get_id(self):
    return self.enr_id
  

#object of Student class
student1= Student("Dhruv",5105133)
student1.get_name()
You can also try this code with Online Python Compiler
Run Code

Data members(attributes)

Attributes are variables of a class. Attributes are public and can be accessed using the dot(.) operator. For example, to access the department attribute of the Student class we can use Student.department.

Self parameter

All class methods have self as their first parameter. We don’t provide any value to this parameter when we call a method. It is similar to this reference in C++, Java.

Constructor in Python: __init__( ) method

init method works as a constructor in Python. It is used to initialize objects of a class. This method is executed at the time of object creation.

# constructor in Python for Student class
def __init__(self,name,enr_id):
    #instance variable
    self.name=name
    self.enr_id=enr_id
You can also try this code with Online Python Compiler
Run Code

Also See, Intersection in Python

Object

Object is an instance of a class. Class is just a concept but an object is a real entity. All objects of a class have their own unique state that is determined by the values of their attributes. All instances of a class share its attributes and methods. When an object is created, a class is said to be instantiated. 

Creating object

#objects of Student class
student1= Student("Dhruv",5105133)
student1.get_name()

student2= Student("Kriti",5105134)
student1.get_name()
You can also try this code with Online Python Compiler
Run Code

Two objects of student class are instantiated with different values. So, we can say both objects are in a unique state. 

It is said that “Everything in Python is an object”. It can be justified as every entity in Python has its attributes and associated functionality called methods that can be accessed by the dot(.) operator.

Let’s say if we consider list type.

l =[5,6,7,8]
l.__len__() ## returns length of the list = 4
dir(l) ## to get list of all attributes and methods of list object 
You can also try this code with Online Python Compiler
Run Code

Output

['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

 

You can practice by yourself with the help of online python compiler.

Know What is Object in OOPs here in detail.

Check out Escape Sequence in Python

FAQs

Q1. What is an instance variable?

Variables whose values are assigned inside the init method(constructor) or method with self, are called instance variables.

Q2. Is there any access modifier in the Python class?

Yes, there are three types of access modifiers. Such as Public, Private, Protected.

By default, members of a class are public.

Key Takeaways

This article covered concepts of class and object in Python with its code implementation.

We recommend you to check out the Coding Ninjas Studio Guided Path on Python programming for learning Python language from basics.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 

Happy learning!

Live masterclass