Stop 07 · Object-Oriented ProgrammingTopics · 2/10

The __init__ Method & self

Part of the Python Roadmap

In short

Every object needs a starting state: a student needs a name, a bank account needs a balance. __init__ is where you set that up, and self is how each object keeps track of its own data.

__init__ runs automatically when an object is created and sets up its starting attributes. self is the object itself, passed in as the first parameter of every instance method.

The jaadu fills in name tags on a row of identical toy robots fresh off a little assembly line, each robot getting its own tag.

Think of it like this

Filling in a newborn's details

When a baby is born, the hospital immediately fills in a form: name, weight, date of birth. Every baby gets their own form. __init__ is that form being filled in the moment an object is created, and self is 'this particular baby', not every baby in the ward.

Watch it happen

What happens on Student('Meera', 21)

Create
__init__
Set self.x
Ready

1/4Python creates a brand new, empty Student object in memory.

The big ideas

What you need to know

Runs automatically

You never call __init__ yourself. Writing Student('Meera', 21) triggers it.

self is this object

self is just the first parameter of every instance method. Python passes the object in for you.

Attributes live on self

self.name = name creates an attribute. A plain name = name would vanish when __init__ ends.

Defaults and validation

Give parameters defaults like city='Delhi', and raise ValueError early if a value doesn't make sense.

See it in code

Walk through the code

python
1class Student:2    def __init__(self, name: str, age: int, city: str = "Delhi"):3        if age <= 0:4            raise ValueError("Age must be positive")5        self.name = name6        self.age = age7        self.city = city8        self.subjects: list[str] = []9 10    def enrol(self, subject: str) -> None:11        self.subjects.append(subject)12 13    def intro(self) -> str:14        return f"I'm {self.name}, {self.age}, from {self.city}"15 16meera = Student("Meera", 21, "Kochi")17arjun = Student("Arjun", 19)18 19meera.enrol("Python")20print(meera.intro())      # I'm Meera, 21, from Kochi21print(arjun.intro())      # I'm Arjun, 19, from Delhi22print(meera.subjects)     # ['Python']23print(arjun.subjects)     # []

1/6self comes first. city has a default, so it's optional when creating a student.

Two students from one class, each with their own name, city and subject list.

Save yourself the pain

Common mistakes

Oops

Forgetting self in a method: def intro(): ...

Do this instead

Every instance method needs self as its first parameter, or you'll get 'takes 0 positional arguments but 1 was given'.

Oops

Writing name = name inside __init__ instead of self.name = name.

Do this instead

Without self., it's a local variable that disappears. Store it on self to keep it.

Oops

Spelling it __int__ or _init_.

Do this instead

It's two underscores, init, two underscores: __init__. A typo means it never runs.

Your mission

Model a mobile phone

~20 min

Quick check

In p = Point(3, 4), what is self inside Point.__init__?

Go deeper

  1. 1Write a BankAccount class whose __init__ takes an owner and an opening balance.
  2. 2Create two accounts and confirm each keeps its own balance.
  3. 3Remember to include self as the first parameter of every method.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge