The __init__ Method
__init__() is called immediately after an object is created. The first parameter self refers to the newly created instance.
class Dog:
def __init__(self, name, breed, age):
self.name = name # Instance attribute
self.breed = breed
self.age = age
self.tricks = [] # Default empty list
# Creating objects — __init__ runs automatically
rex = Dog("Rex", "German Shepherd", 3)
buddy = Dog("Buddy", "Labrador", 5)
print(rex.name) # Rex
print(buddy.age) # 5Understanding self
self is a reference to the instance being created. It is not a keyword — you could name it anything — but self is the universal Python convention.
class Circle:
def __init__(self, radius):
self.radius = radius # "this circle's radius"
self.area = 3.14 * radius**2 # Computed on creation
c1 = Circle(5)
c2 = Circle(10)
print(c1.radius, c1.area) # 5 78.5
print(c2.radius, c2.area) # 10 314.0Default Constructor Arguments
Use default values for optional attributes.
class User:
def __init__(self, username, email, role="user", active=True):
self.username = username
self.email = email
self.role = role
self.active = active
admin = User("alice", "alice@example.com", role="admin")
guest = User("bob", "bob@example.com")
print(admin.role) # admin
print(guest.role) # user (default)Validation in __init__
Validate arguments inside __init__ to prevent invalid objects from being created.
class BankAccount:
def __init__(self, owner, balance=0):
if not owner:
raise ValueError("Owner name required")
if balance < 0:
raise ValueError("Balance cannot be negative")
self.owner = owner
self.balance = balance
try:
bad = BankAccount("", -100)
except ValueError as e:
print(f"Error: {e}")
good = BankAccount("Alice", 1000)
print(f"{good.owner}: ${good.balance}")Class Attributes vs Instance Attributes
Class attributes are shared across all instances. Instance attributes are unique per object.
class Employee:
company = "Algorid Limited" # Class attribute
employee_count = 0
def __init__(self, name, salary):
self.name = name # Instance attribute
self.salary = salary
Employee.employee_count += 1 # Modify class attr
e1 = Employee("Alice", 75000)
e2 = Employee("Bob", 80000)
print(e1.company) # Algorid Limited (class attr)
print(Employee.employee_count) # 2