Classes and Objects in Python
Object-Oriented Programming (OOP) is one of the most powerful paradigms in Python.
If you want to build scalable applications, crack interviews, or understand frameworks like Django deeply, mastering these fundamentals is non-negotiable.
Class
A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.
A class is a blueprint for creating objects.
Syntax:
class ClassName:
passExample:
class Person:
passHere:
- class is the keyword.
- Person is the class name (by convention, class names use PascalCase).
- pass means “do nothing for now.”
At this stage, the class doesn’t store any data or perform any action. It’s just a structure.
Creating Class
Classes are created using class keyword. Attributes are variables defined inside class and represent properties of the class. Attributes can be accessed using dot . operator
Object
An object is a specific instance of a class. It holds its own set of data (instance variables) and can invoke methods defined by its class. Multiple objects can be created from same class, each with its own unique attributes.
Example:
class Person:
pass
p1 = Person() # Creating object from classHere:
- p1 is an object.
- Person() calls the class to create a new instance.
You can create the multiple objects:
p2 = Person()
p3 = Person()Each object is independent.
The __init__() Constructor
The __init__() method acts as a constructor in Python and is automatically executed when an object is created. It is used to initialize the attributes of the object with the values provided at the time of object creation.
Why Do We Need It?
- Because most objects need initial values.
Example:
class Person:
def __init__(self,name,age):
self.name = name
self.age = ageNow create objects:
p1 = Person("ABC",25)
p1 = Person("DEF",30)
print(p1.name)
print(p1.age)What Happens Internally?
When you write:
p1 = Person("ABC",25)Python internally does something like:
Person.__init__(p1, "ABC", 25)so:
- self → refers to p1
- “ABC” → assigned to self.name
- 25 → assigned to self.age
The constructor ensures each object gets its own data.
Understanding the ‘self’ Keyword
self refers to the current object.
It allows an object to:
- Store its own data
- Access its own methods
- Differentiate between different instances
Important points:
- self is not a keyword — it’s a convention.
- It must always be the first parameter of instance methods.
- It represents the calling object.
Example:
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Name:", self.name)Usage:
s1 = Student("ABC")
s1.display()Here:
- self.name refers specifically to s1’s name.
- If we create another object, it will have its own separate data.
Instance variables
Instance variables are variables that belong to an object.
They are defined using self.
Get Saurabh Gandhi’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
class Car:
def __init__(self, brand, price):
self.brand = brand
self.price = priceHere:
- brand and price are instance variables.
- Every object will have its own copy.
Example:
c1 = Car("BMW", 5000000)
c2 = Car("Audi", 4500000)
print(c1.brand) # BMW
print(c2.brand) # AudiEach object maintains its own state.
Instance Methods
Instance methods are functions defined inside a class that operate on instance variables.
They always include self as the first parameter.
Example:
class BankAccount:
def __init__(self, holder, balance):
self.holder = holder
self.balance = balance
def deposit(self, amount):
self.balance += amount
def check_balance(self):
print("Current Balance:", self.balance)Usage:
acc1 = BankAccount("ABC", 10000)
acc1.deposit(2000)
acc1.check_balance()The method deposit() modifies that specific object's balance.
Object Lifecycle in Python
Understanding object lifecycle is crucial for advanced programming.
An object goes through these stages:
1. Creation
When you call the class:
p1 = Person("ABC", 25)- Memory is allocated.
- __init__() initializes the object.
2. Usage
The object interacts with methods:
p1.greet()During this phase:
- Attributes can be modified.
- Methods can be called.
- Object participates in program logic.
3. Destruction
When an object is no longer referenced, Python’s garbage collector removes it.
You can manually delete:
del p1There is also a special method called __del__() (destructor):
class Demo:
def __del__(self):
print("Object destroyed")But in modern Python, manual memory management is rarely required.
Python automatically handles memory cleanup using garbage collection.
Putting It All Together
class Laptop:
def __init__(self, brand, ram):
self.brand = brand
self.ram = ram
def upgrade_ram(self, extra):
self.ram += extra
def display(self):
print(f"Brand: {self.brand}, RAM: {self.ram}GB")Usage:
l1 = Laptop("Dell", 8)
l1.display()
l1.upgrade_ram(8)
l1.display()This example demonstrates:
- Class creation
- Object creation
- Constructor
- Instance variables
- Instance methods
- State modification
Final Summary
Questions
- What is a class in Python?
- What is an object?
- What is the difference between a class and an object?
- How do you create a class in Python?
- How do you create an object from a class?
- What is the purpose of the __init__() method?
- What is the role of the self keyword?
- What are instance variables?
- What are instance methods?








