2.1.2 · D1OOP Fundamentals

Foundations — `__init__` constructor — initializing attributes

1,779 words8 min readBack to topic

This is the "zero prerequisites" companion to the parent note. If a word there confused you — self, dunder, attribute, instance, None — it is defined here, from the ground up, with a picture for each.


0. What is an object? (the very first picture)

Before __init__ can mean anything, you must picture what it fills.

Look at the figure: the box on the left is empty (just allocated memory, no data yet). The box on the right is filled — it now carries name = "Asha" and age = 30. __init__'s entire job is to turn the left box into the right box.

Figure — `__init__` constructor — initializing attributes

Why the topic needs this: the parent note keeps saying "fill an already-created object." You cannot picture "fill" until you picture the empty-vs-full box.


1. What is a class? (the blueprint)

Figure — `__init__` constructor — initializing attributes

In the figure, the single teal blueprint on the left produces three orange houses on the right. They share the shape (both have a name slot and an age slot) but hold different values.

Why the topic needs this: __init__ is written once inside the class but runs once per object. Knowing which is which is the whole game.


2. What is an instance?

Recall Quick check

If Person is the class, how many instances are in a = Person("Asha", 30) and b = Person("Ravi", 25)? ::: Two instances, both built from the one Person class.

Why the topic needs this: the parent note distinguishes class attributes (on the blueprint) from instance attributes (on each house). __init__ sets instance ones. See Class vs Instance Attributes.


3. What is an attribute? (the dot .)

The dot . is not decoration — it is an address arrow. p.name says: go to box p, then find the slot labelled name inside it.

Why the topic needs this: self.name = name — the single most important line in __init__ — is entirely built out of dots. You cannot read it until the dot is a picture, not a symbol.


4. What is self? (the "this box" pronoun)

Now we can define the word that trips everyone up.

Figure — `__init__` constructor — initializing attributes

In the figure, the same instruction sheet (self.name = name) is applied to two different bags on the conveyor. The word self re-points to whichever bag is under the nozzle — first Asha's, then Ravi's.

So self.name = name reads: on THIS box, in the slot name, store the value the caller handed in.

Why the topic needs this: self appears in every line of every __init__. It is the pronoun that turns one written recipe into per-object action.


5. What is = here? (assignment, not equality)

So in self.name = name:

  • Right side name — the value handed in (say "Asha").
  • Left side self.name — the slot on this box.
  • The = copies the value into the slot.

Why the topic needs this: the parent's biggest gotcha is name = name vs self.name = name. That distinction is purely about the direction of = and the meaning of the dot.


6. What is a parameter / argument?

Recall Match them up

In Person("Asha", 30), which is the argument for the parameter age? ::: 30.

Why the topic needs this: __init__(self, name, age) only makes sense once you see name/age as empty labelled slots waiting to be filled by the caller's arguments.


7. What is None? (the "nothing" value)

Why the topic needs this: the parent insists __init__ must return None. That just means: __init__'s job is to do setup, not to produce a result — so it hands back "nothing." Also, the safe fix for the mutable-default trap uses items=None as a "was anything given?" flag. See Mutable default argument pitfall.


8. What is a dunder (double-underscore) name?

Why the topic needs this: __init__ is a dunder. Understanding that it is auto-invoked (not called by you) resolves the parent's Mistake 2 (calling Person.__init__(...) directly). See Dunder methods.


How these feed the topic

Object = box in memory

Attribute = slot reached by dot

Class = blueprint

Instance = one built object

self = this object

Assignment = right to left

Parameter and argument

__init__ fills the object

None = nothing returned

Dunder = auto-called method

__init__ constructor topic

Read it bottom-up: boxes and blueprints give you objects; the dot gives you attributes; self plus assignment plus parameters let you fill those attributes; None and "dunder" explain how the fill runs and ends. Together they are __init__.


Equipment checklist

  • What is an object? ::: A labelled box in memory holding data (and its actions).
  • Class vs instance? ::: Class is the blueprint; an instance is one object built from it.
  • What does the dot . do in p.name? ::: Goes to box p and finds the slot named name inside it.
  • What is self? ::: A placeholder for "the particular object being worked on right now."
  • Is self a keyword? ::: No — it's a conventional name for the auto-passed first parameter.
  • What does = mean in self.name = name? ::: Put the right-hand value into the left-hand slot (right-to-left), not maths equality.
  • Why are the two names in self.name = name different? ::: Left (with dot) is an attribute slot on the object; right is a local input value.
  • Parameter vs argument? ::: Parameter is the named slot in the definition; argument is the actual value passed in.
  • What is None? ::: Python's value for "nothing / no value."
  • What is a dunder, and who calls it? ::: A double-underscore special method that Python calls automatically at defined moments.

Connections