In Python:
👉 Class = Blueprint
👉 Object = Instance created from blueprint
A class defines:
✔ Data (attributes)
✔ Behavior (methods)
An object is the actual entity in memory.
Think of:
- Class → Design of a Car 🚗
- Object → Actual Car Built
You can build many objects from one class.
A class is defined using the class keyword.
class Person:
passpass means “empty placeholder”
Objects are created by calling the class like a function.
p1 = Person()
p2 = Person()Each object:
✔ Lives in memory
✔ Has a unique identity
print(id(p1))
print(id(p2))Attributes are variables stored inside objects.
Defined inside the constructor.
class Person:
def __init__(self, name, age):
self.name = name
self.age = agep1 = Person("Alice", 25)
print(p1.name)
print(p1.age)Output:
Alice
25
Each object gets its own copy.
Defined directly inside the class.
class Person:
species = "Human"p1 = Person()
p2 = Person()
print(p1.species)
print(p2.species)Shared across all objects.
| Feature | Instance Attribute | Class Attribute |
|---|---|---|
| Belongs To | Object | Class |
| Shared? | ❌ No | ✅ Yes |
| Memory | Per object | Single copy |
Methods are functions defined inside classes.
class Person:
def greet(self):
print("Hello!")p1 = Person()
p1.greet()p1.greet() → Person.greet(p1)
Python automatically passes the object.
self refers to:
👉 The current object calling the method
class Person:
def greet(self):
print(self)Constructor runs automatically during object creation.
class Person:
def __init__(self):
print("Object created!")p1 = Person()Output:
Object created!
Objects store state (data).
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1c1 = Counter()
c1.increment()
c1.increment()
print(c1.count)Output:
2
Python allows attributes to be added dynamically.
class Person:
pass
p1 = Person()
p1.name = "Alice"
print(p1.name)Flexible but dangerous in large systems.
print(p1.__dict__)Shows stored data.
print(Person.__dict__)def method(self)Works with objects.
Works with class itself.
@classmethod
def method(cls)No object / class access.
@staticmethod
def method()| Method Type | Access |
|---|---|
| Instance Method | Object data |
| Class Method | Class data |
| Static Method | Utility logic |
Checks memory location.
a = [1, 2]
b = a
print(a is b)Output:
True
Checks values.
a = [1, 2]
b = [1, 2]
print(a == b)
print(a is b)Output:
True
False
Objects:
✔ Created → __init__()
✔ Used → Methods
✔ Destroyed → Garbage Collector
Each object:
✔ Separate identity
✔ Separate instance attributes
Class attributes → Shared memory
| Error | Cause |
|---|---|
TypeError: missing self |
Forgot self parameter |
AttributeError |
Accessing non-existent attribute |
| Overwriting class attribute | Accidentally shadowing |
| Mutable class attributes bug | Shared mutation |
class Person:
hobbies = []p1 = Person()
p2 = Person()
p1.hobbies.append("Coding")
print(p2.hobbies)Output:
['Coding']
🚨 Shared across objects → subtle bug
✔ Use instance attributes for object data
✔ Avoid mutable class attributes
✔ Always include self
✔ Use constructor for initialization
✔ Keep responsibilities clear
| Concept | Meaning |
|---|---|
| Class | Blueprint |
| Object | Instance |
| Instance Attribute | Per object data |
| Class Attribute | Shared data |
| Method | Behavior |
| self | Current object |
| init | Constructor |
| dict | Internal storage |
- Create a
Carclass with attributes & methods - Create multiple objects & compare identities
- Implement instance vs class attributes
- Demonstrate mutable class attribute bug
- Inspect
__dict__ - Write class method & static method
- Build stateful object (Counter, BankAccount)