2.1.1OOP Fundamentals

Class vs object — blueprint vs instance

1,582 words7 min readdifficulty · medium1 backlinks

WHAT are we talking about?


WHY does this distinction exist?

WHY split blueprint from instance?

  1. Reuse — write the description once, stamp out many objects.
  2. Independent state — each object keeps its own data; changing one doesn't corrupt others.
  3. Shared behaviour — methods are stored once (on the class), so 1000 objects don't each store 1000 copies of the code.

HOW do you build and use them?

class Dog:                 # blueprint begins
    species = "Canis"      # CLASS attribute (shared by all dogs)
 
    def __init__(self, name, age):   # the constructor
        self.name = name             # INSTANCE attribute (per object)
        self.age  = age
 
    def bark(self):                  # behaviour
        return f"{self.name} says Woof!"
 
d1 = Dog("Rex", 3)         # instantiation -> an OBJECT
d2 = Dog("Fido", 5)        # another OBJECT, independent state

HOW instantiation works, step by step:

  1. Dog("Rex", 3) asks Python to create a blank object of type Dog.
  2. Python calls __init__ with self = the new blank object.
  3. self.name = name writes data into that object.
  4. The finished object's reference is handed back and stored in d1.
Figure — Class vs object — blueprint vs instance

Worked Examples


Common Mistakes


Recall Feynman: explain to a 12-year-old

A class is like a cookie cutter. The cutter is not a cookie — it's the shape. Every time you press it into dough you get a cookie (an object). All the cookies have the same shape (same class), but you can sprinkle different toppings on each one (different data). Throwing one cookie away doesn't hurt the others, and you still have the cutter to make more.


Flashcards

What is a class?
A blueprint/template that bundles data (attributes) and behaviour (methods); it describes a kind of thing but is not itself a runtime instance.
What is an object?
A concrete instance built from a class that occupies memory and has its own copy of instance attributes.
What is instantiation?
The act of creating an object from a class, e.g. Dog("Rex", 3), which triggers __init__.
Where do instance attributes live?
On the individual object (written via self.x = ...), separate per object.
Where do class attributes live?
On the class itself, shared by all instances unless shadowed on an instance.
Why doesn't changing d1.age affect d2.age?
Each object has its own independent memory for instance attributes.
What does a is b test for two equal-looking objects?
Identity (same memory location) — it's False for two separately instantiated objects even with equal data.
Does writing a class create any object?
No — it only defines a blueprint; objects appear only when you call the class.
Why store methods on the class, not each object?
To share one copy of the code across all instances, saving memory.

Connections

  • Constructors and __init__ — how an object is initialised at instantiation.
  • self and instance methods — how self binds methods to a specific object.
  • Class vs instance attributes — the shared-vs-own data distinction.
  • Encapsulation — why bundling data+behaviour into a class matters.
  • Identity vs Equality (is vs ==) — distinguishing objects.
  • Memory model and references — where objects actually live.

Concept Map

describes template for

instantiation via

creates

stores once

holds

owns copy of

calls

run on

shared across

enables

keeps

so changing one

Class - blueprint

Object - instance

__init__ constructor

Methods - shared behaviour

Class attribute - shared

Instance attributes - per object

Reuse - many from one

Independent state

Does not affect others

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, class ek blueprint hota hai — jaise architect ka drawing ya cookie cutter. Wo sirf describe karta hai ki cheez kaisi dikhegi: uske paas kaunse data (attributes) honge aur kaunse kaam (methods) wo kar sakti hai. Lekin class khud koi real cheez nahi hai, sirf plan hai. Jab tak aap Dog("Rex", 3) likh kar use call nahi karte, koi actual dog memory mein nahi banta.

Jab aap class ko call karte ho, tab ek object (instance) banta hai — yahi asli cheez hai jo memory mein rehti hai. Is process ko instantiation kehte hain. Har object ke paas apna alag data hota hai. d1.age = 4 karne se sirf d1 change hota hai, d2 ko kuch nahi hota — kyunki instance attributes har object ke andar alag-alag store hote hain. Yeh self.name = name wali line har naye object ke liye fresh chalti hai.

Ek important baat: methods aur class attributes class par store hote hain, ek hi copy, sabhi objects share karte hain. Isliye 1000 dogs banao to code 1000 baar copy nahi hota — memory bachti hai. Aur identity ka funda yaad rakho: a is b check karta hai ki dono same memory cell hain ya nahi. Do alag Dog("Rex",3) banao, data same ho tab bhi a is b False hoga, kyunki wo do alag objects hain.

Yeh distinction OOP ki neev hai. "Class = Cutter, Object = Cookie" yaad rakhna — ek cutter se kitni bhi cookies, sab same shape, par har ek pe alag sprinkles. Ye samajh gaye to constructors, self, encapsulation sab clear ho jayega.

Go deeper — visual, from zero

Test yourself — OOP Fundamentals

Connections