OOP, Classes/Objects, __init__ & self
Object-Oriented Programming models real things as objects with data (attributes) and behavior (methods).
Python OOP
Object-Oriented Programming models real things as objects with data (attributes) and behavior (methods).
OOP helps organize large codebases — each class owns its logic.
Real-life example: OOP is a school — Student and Teacher are different roles with different duties but same campus rules.
Python Classes/Objects
A class is a blueprint; an object is one instance built from it. Create objects by calling ClassName().
Attributes store state; methods define actions on that state.
Real-life example: Cookie cutter (class) vs one actual cookie (object) — same shape, each can have different icing.
class Dog:
species = "Canis"
def bark(self):
return "Woof!"
my_dog = Dog()
print(my_dog.bark())Python __init__ Method
__init__ runs when a new object is created. It sets starting values for attributes.
It is the constructor — not named 'constructor' in Python but serves the same role.
Real-life example: __init__ is filling a new employee form on day one — name, ID, and desk assigned immediately.
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
acct = BankAccount("Ananya", 1000)
print(acct.owner, acct.balance)Python self Parameter
self is the first parameter of instance methods — it refers to the current object.
Python passes the object automatically when you call account.deposit(50).
Real-life example: self is saying 'my own wallet' — each person reaches into their pocket, not someone else's.
class Counter:
def __init__(self):
self.count = 0
def add(self, n=1):
self.count += n
return self.count
c = Counter()
print(c.add(), c.add(5))