2.1.5 · D1OOP Fundamentals

Foundations — `self` — what it is and how Python passes it

1,462 words7 min readBack to topic

This page assumes nothing. Before we can even talk about self (that job belongs to the parent note, the parent topic), we must earn every word the parent leaned on: class, instance, attribute, method, argument, the dot ., binding. We build them one at a time, each one a picture.


1. Object — a labelled box that holds stuff

Picture it. Look at the figure: one box named alice. Inside are two drawers, owner and balance. The drawers are its own — no other box shares them.

Figure — `self` — what it is and how Python passes it

Why the topic needs it. self will turn out to be one of these boxes. You cannot understand "the method works on self" until "an object is a box with its own drawers" is second nature.


2. Attribute — one named drawer inside the box

The dot here means "go inside this box and find the drawer called…". So alice.balance means "inside the box alice, the drawer labelled balance".

More depth on where these drawers live: Instance vs Class Attributes.


3. Class — the blueprint many boxes share

Picture it. The figure shows one blueprint sheet (Account) on the left, and two finished boxes (alice, bob) built from it on the right. Same plan, two separate boxes, each with its own drawer values.

Figure — `self` — what it is and how Python passes it

Why the topic needs it. The whole reason self exists is that one blueprint is shared by many boxes. If each object had its own private copy of the code, there'd be no "which one?" question to answer. Foundation topic: Classes and Instances.


4. Instance — one actual box made from the blueprint

Class and instance are the two halves you must keep separate:

Blueprint (the plan)
the class, e.g. Account
One built box (has real values)
an instance, e.g. alice

5. Function and argument — a machine you feed values into

Before "method", we need "function".

Picture it. The figure shows a machine deposit with two slots on its left labelled self and amount. You drop values into the slots; that filling-in is called binding (the parameter binds to the argument you passed).

Figure — `self` — what it is and how Python passes it

Why the topic needs it. The parent's big reveal — alice.deposit(50) becomes Account.deposit(alice, 50) — is pure argument binding. If "first value fills first slot" is solid, that reveal is obvious rather than magic.


6. Method — a function that lives inside the blueprint

So a method is nothing new: it is a function (§5) stored inside a class (§3). The only special thing is a habit: its first parameter slot is reserved for the box itself, and by convention we name that slot self.

Deeper: Bound and Unbound Methods.


7. Binding via the dot — how the box gets into the first slot

Now assemble it. You write:

The dot (§2, "look inside alice") finds the deposit machine, and — here is the one new rule — it remembers alice and drops it into the first slot for you. The result is exactly:

Picture it. The figure shows the dot acting as a hand that grabs the box on its left and pushes it into the self slot before your other arguments arrive.

Figure — `self` — what it is and how Python passes it

The machinery that makes the dot behave this way is the Descriptor Protocol; a first-slot-less cousin (cls, no first arg) shows up in classmethod and staticmethod.


Prerequisite map

Object = box with drawers

Attribute = one named drawer

Class = blueprint for boxes

Instance = one built box

Function = machine with slots

Argument fills a slot by position

Method = function inside a class

Dot fills first slot with the box

self = that box, inside the method


Equipment checklist

An object is best pictured as…
a labelled box holding its own named drawers (data) plus things it can do (behaviour).
Reading alice.balance out loud, the dot means…
"go inside alice and find the drawer called balance" — i.e. "alice's balance".
A class differs from an instance because…
a class is the blueprint/plan; an instance is one real box built from it.
An argument is bound to a parameter by…
position — the first value fills the first slot, the second fills the second, and so on.
A method is really just…
a function written inside a class, with its first slot reserved for the calling object.
When you write alice.deposit(50), the dot's special job is…
to grab alice and drop it into the first parameter slot automatically.
So alice.deposit(50) is equivalent to the explicit call…
Account.deposit(alice, 50).
The single new idea self adds on top of all the above is…
nothing — self is just the name of that auto-filled first slot, holding the box that called the method.

Connections