📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Class and Object in Python: Complete Guide

Quick answer: Learn Python classes and objects including attributes, methods, the __init__ constructor, self, and the difference between class and instance variables.

What is a Class?

A class is a blueprint for creating objects, defining what attributes and methods those objects will have. An object is a specific instance created from that class.

class Student:
    def __init__(self, name, course):
        self.name = name
        self.course = course

    def introduce(self):
        return f"I am {self.name}, studying {self.course}"

s1 = Student("Priya", "Data Science")
print(s1.introduce())   # I am Priya, studying Data Science

The __init__ Constructor

__init__ runs automatically when a new object is created, typically used to set up the object's initial attributes.

class Course:
    def __init__(self, name, fee):
        self.name = name
        self.fee = fee

c = Course("Data Analytics", 65000)
print(c.name, c.fee)

Understanding self

self refers to the specific object the method is being called on. It must be the first parameter of every instance method, though Python passes it automatically, so you never supply it explicitly when calling the method.

class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1   # self.count refers to THIS object's count

c1 = Counter()
c2 = Counter()
c1.increment()
c1.increment()
print(c1.count)   # 2
print(c2.count)   # 0, unaffected by c1

Class Variables vs Instance Variables

class Student:
    school = "Fireblaze AI School"   # class variable, shared by all instances

    def __init__(self, name):
        self.name = name              # instance variable, unique per object

s1 = Student("Aman")
s2 = Student("Neha")

print(s1.school, s2.school)   # both print "Fireblaze AI School"
print(s1.name, s2.name)       # Aman, Neha (different per object)

Instance Methods

A method is a function defined inside a class that operates on a specific object's data, accessed through self.

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient balance")

account = BankAccount(1000)
account.deposit(500)
account.withdraw(200)
print(account.balance)   # 1300

Common Interview Questions

What is the difference between a class and an object?

A class is the blueprint or template. An object is a specific instance created from that class, with its own actual data.

Why does every method need self as its first parameter?

self lets the method know which specific object's data to operate on, since a class can have many objects, each with its own independent state.

What is the difference between a class variable and an instance variable?

A class variable is shared across every object created from the class. An instance variable is specific to each individual object.

FAQ

Frequently Asked Questions

What is the difference between a class and an object in Python?

A class is a blueprint defining attributes and methods. An object is a specific instance created from that class, holding its own actual data.

What does __init__ do in a Python class?

It is the constructor method that runs automatically when a new object is created, typically used to set the object's initial attribute values.

Why do Python methods need self as the first parameter?

self refers to the specific object the method is being called on, letting the method access and modify that particular object's data.

What is the difference between a class variable and an instance variable?

A class variable is shared by every object created from the class. An instance variable is unique to each individual object.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles