Concepts / Classes and Objects: The Basics

Classes and Objects: The Basics

__init__ is a special method that runs automatically when an object is instantiated, marking the beginning of the object's lifecycle.

  • Programming

The First Moment of an Object

When a new object is created from a class, Python automatically calls a special method named __init__. This method marks the beginning of the object's lifecycle and gives you a place to establish the object's initial state.

What do you think happens?

Suppose a class contains an __init__ method and you create a new instance from that class. When does __init__ run?

  • Only when the program finishes
  • Automatically when the new instance is created
  • Only after you call __init__ by its full name
  • Only when another method changes an attribute
Reveal answer

Answer: Automatically when the new instance is created

__init__ is automatically called whenever a new instance of the class is created. Its job is to initialize the object's attributes to sensible starting values.

From Class to Initialized Instance

A class provides the structure from which objects are created. The important execution path is straightforward: the class is defined, the class is called to create an instance, and the new instance's __init__ method runs automatically. During that call, __init__ assigns the attributes that belong to that particular object.

definescreates instanceinitializes attributesClass definitionClass call__init__Initialized object
How does control flow from defining a class to producing an initialized object?
class is calledautomatic callattributes initializedClassNew instance__init__Initial state
What happens next when a class is called to create an object, and when does __init__ run?

Writing the Initialization Method

An __init__ method receives self as its first parameter. self refers to the specific object being created. Assigning an attribute through self attaches that attribute to that particular instance. A useful __init__ method initializes every attribute the object will use, so each instance begins in a valid and predictable state.

class Profile: def __init__(self, name): self.name = name self.active = True

self identifiesownsself identifiesowns__init__Profile Aname, activeProfile Bname, active
How does self identify the particular object whose instance attributes are being created or changed?

Seeing the Initial State

Two independent profiles

Use the Profile class to create two objects with different names. Determine what __init__ establishes for each object.

Create the first instance: Calling the class creates one Profile instance. Python automatically runs __init__ for that instance, with self referring to it and name receiving the first name.

Initialize its attributes: The method assigns the first object's name attribute and sets its active attribute to True.

Create the second instance: Calling the class again creates another Profile instance. Its own call to __init__ uses a different self, so the second object receives its own attributes.

Compare the objects: Both objects have the attributes initialized by __init__, but each instance has its own separate set of attributes and can have a different name.

Each instance begins with a predictable initial state, and the attributes initialized for one instance are separate from the attributes initialized for the other.

before __init__ finishes__init__ assignsinitial stateNew ProfileNo initializedattributes shownProfilename, active
What changes inside a newly created object as __init__ assigns its initial attributes?

The before-and-after view is about state, not about creating a second class. Before initialization is complete, the object's intended attributes have not yet been established by the method. After __init__ assigns them, the object has the predictable starting state that later code can use.

Class Reuse and Instance Independence

A class can be used to create multiple instances. Those instances come from the same class, but each one has its own separate set of attributes. The class supplies the shared definition of how initialization works; each call that creates an instance gives __init__ a particular self and initializes that instance independently.

ClassInstance
Defines the object structure and initialization behaviorIs one particular object created from the class
Provides the __init__ methodReceives its own call to __init__
Can be used to create multiple objectsHas its own separate set of attributes
automatic callattributes initializedInstance created__init__ runningInitial stateestablished
What stages does an object pass through from instantiation to having its initial usable state?

Mistakes with __init__

  • Assuming __init__ must be called manually for normal object creation

    __init__ runs automatically whenever a new instance is created.

    Fix: Place the object's starting attribute assignments in __init__ and let object creation trigger the method.

  • Treating self as a general-purpose placeholder unrelated to the object

    self refers to the specific object being created, so it determines which instance receives the attribute.

    Fix: Read self.attribute as an attribute attached to the current instance.

  • Expecting two instances to share one independent set of attributes

    Each instance has its own separate set of attributes, initialized by its own call to __init__.

    Fix: Trace each instance's own __init__ call and its own self reference.

  • Leaving an attribute out of __init__ even though the object will use it

    The source recommends initializing all attributes an object will use so every instance starts in a valid, predictable state.

    Fix: Initialize every attribute the object is expected to use inside __init__.

Practice the Execution Trace

MEDIUM

Consider a class named Account with an __init__ method that assigns self.owner and self.open. Trace what happens when two Account instances are created with different owner values. Identify when __init__ runs, what self refers to during each call, and why the two instances have separate owner and open attributes.

Hints
  • Start with the first class call and identify the instance receiving the first self reference.
  • Repeat the trace for the second class call rather than reusing the first instance's self.
  • Check that every attribute used by the object receives an initial value in __init__.

When designing a class, list the attributes an object needs at its initial state, then initialize all of them in __init__. This makes the starting state of every instance valid and predictable and makes the execution trace easier to follow.

Key Takeaways

  • __init__ is the constructor for a Python class and runs automatically when a new instance is created.
  • The self parameter refers to the specific object being initialized.
  • Assignments through self create or initialize attributes for that particular instance.
  • Each instance has its own separate set of attributes and its own call to __init__.
  • Initialize every attribute an object will use in __init__ so each instance starts in a valid, predictable state.