Course Progress56%
🍎 Python OOP Topic 56 / 100
⏳ 8 min read

Class vs Instance Variables

Data that belongs to each individual object vs data shared across all objects of a class — and why the difference matters.

"Instance variables are each object's private notebook. Class variables are the shared whiteboard everyone in the class reads from."

— ShurAI

Two Kinds of Variables

Instance Variable
Defined with self.name = ...
Unique to each object
Changing one doesn’t affect others

Example: each student’s own name
Class Variable
Defined directly inside the class
Shared by all objects of that class
Changing it affects every instance

Example: school name all students share
python
class Student:
    school = "ShurAI Academy"   # CLASS variable — shared by all

    def __init__(self, name, score):
        self.name  = name    # INSTANCE variables — unique to each
        self.score = score

s1 = Student("Riya",  92)
s2 = Student("Arjun", 75)

print(s1.name)    # Riya    — unique to s1
print(s2.name)    # Arjun   — unique to s2
print(s1.school)  # ShurAI Academy — shared
print(s2.school)  # ShurAI Academy — same for everyone

Changing a Class Variable

Change it via the class name and every instance sees the new value immediately:

python
Student.school = "New Academy"   # change via class name

print(s1.school)  # New Academy
print(s2.school)  # New Academy  — both updated!
⚠️ Watch out: assigning via an instance creates a new instance variable

If you do s1.school = "Other", Python creates a new instance variable on s1 — it does not change the class variable. s2.school is unaffected. This is a common source of confusion.

Counting Instances with a Class Variable

A classic use of class variables is tracking how many objects have been created:

python
class Robot:
    count = 0   # class variable — shared counter

    def __init__(self, name):
        self.name = name
        Robot.count += 1   # increment shared counter on each new robot

    def describe(self):
        print(f"Robot #{Robot.count}: {self.name}")

r1 = Robot("R2D2")
r2 = Robot("C3PO")
r3 = Robot("BB8")

print(f"Robots created: {Robot.count}")   # 3

Real Example — Product with Shared Tax Rate

python
class Product:
    tax_rate = 0.18   # GST — same for every product

    def __init__(self, name, price):
        self.name  = name
        self.price = price

    def price_with_tax(self):
        return self.price * (1 + Product.tax_rate)

    def display(self):
        print(f"{self.name}: ₹{self.price} + tax = ₹{self.price_with_tax():.2f}")

p1 = Product("Laptop", 50000)
p2 = Product("Phone",  20000)
p1.display()   # Laptop: ₹50000 + tax = ₹59000.00
p2.display()   # Phone:  ₹20000 + tax = ₹23600.00

Product.tax_rate = 0.12   # change once, both products update
p1.display()   # Laptop: ₹50000 + tax = ₹56000.00

"When data changes per object, use an instance variable. When data is the same across all objects and changes together, use a class variable."

— ShurAI

🧠 Quiz — Q1

Where is an instance variable defined?

🧠 Quiz — Q2

You have a class variable Robot.count = 0. You create two robots. How do you correctly increment the shared counter?

🧠 Quiz — Q3

What happens if you do s1.school = "Other" when school is a class variable?

🧠 Quiz — Q4

Which is the best use case for a class variable?