Concepts / Instance and Class Attributes

Instance and Class Attributes

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

  • Programming
Interactive lab

Try it: Classes, Instances and Attributes

How a class creates instances with __init__ and self, why an attribute read checks the instance's __dict__ before the class, why assigning through an instance never changes the class, how a class-level list is shared by every instance, and why obj.method(x) is Class.method(obj, x).

How it works

  1. Dog(...) makes a new, empty instance and calls Dog.__init__(new_object, ...); self.name = ... stores into that object's __dict__.
  2. Reading obj.attr looks in obj.__dict__ first, then in the class.
  3. Assigning obj.attr = value always writes obj.__dict__, so it shadows a class attribute for that one object only.
  4. Mutating a class-level list (self.tricks.append) changes the one list every instance shares.
  5. obj.method is found on the class and bound to obj, so obj.method(x) runs Class.method(obj, x).

Default run (30 steps): Ready: class Dog and the statements you picked. Watch where each attribute is found and where each assignment writes. … Finished. Printed: ['sit'] / Wolf / Canis familiaris / Fido is 5.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. Object numbers are illustrative, not real id() values.

Educational simulation

Loading the simulation…

From Class to Object

A class provides the basis for creating objects, called instances. The important event for this topic happens when a new instance is created: Python automatically runs the special __init__ method. That method begins the object's lifecycle by giving the new object its initial attributes.

ClassdefinitionNew instanceobject__init__automatic callInitial stateattributes setcreatestriggersinitializes
What happens after Python has a class and a program creates a new instance?

What do you think happens?

A class has an __init__ method. When a new instance is created, what should you expect to happen next?

  • The method is called automatically

  • The method runs only if called separately

  • The method is ignored during object creation

Reveal answer
Answer:

The method is called automatically

__init__ is automatically called whenever a new instance of the class is created. Its job is to initialize the object's attributes and begin its initial state.

The Creation Sequence

The sequence can be traced in three stages. First, Python has a class definition. Next, a program creates a new instance from that class. Finally, Python automatically calls __init__ for that new instance. The call to __init__ marks the beginning of the object's lifecycle and gives the method an opportunity to establish sensible starting values.

  1. 1
    A class is available as the basis for creating instances.
  2. 2
    The program creates a new instance of that class.
  3. 3
    Python automatically calls the class's __init__ method.
  4. 4
    __init__ initializes the attributes belonging to that particular instance.
  5. 5
    The instance begins its lifecycle with an initial, predictable state.
Class definitionclass availableInstantiationnew object__init__automatic callObject lifecycleinitial statemakes possiblestartsbegins
Where does __init__ fit when an object's lifecycle begins?

Connecting self to One Object

The self parameter in __init__ refers to the specific object being created. This is what allows initialization to affect one instance rather than all instances produced from the class. When __init__ attaches an attribute through self, that attribute belongs to the particular object represented by self.

__init__initialization methodselfspecific objectInstance Aown attributesrefers throughidentifies
How does self connect __init__ to the particular object being initialized?
Worked example

Two Independent Initializations

Imagine a class used to create profile objects. Two profile instances are created, one for a learner named Amina and one for a learner named Ravi.

  1. 1
    Create the first instance

    Python creates Amina's profile instance and automatically calls __init__. During this call, self refers to Amina's particular profile object.

  2. 2
    Initialize the first instance

    The attributes assigned through self become part of Amina's own initial state.

  3. 3
    Create the second instance

    Python creates Ravi's profile instance and automatically calls __init__ again. This time, self refers to Ravi's particular profile object.

  4. 4
    Initialize the second instance

    The attributes assigned through self become part of Ravi's own initial state, separate from Amina's attributes.

Result

The same __init__ method is used for both objects, but each call initializes a different object because self refers to the specific instance being created.

Building Initial State

An object's initial state is the collection of attribute values it has immediately after __init__ finishes. The method's purpose is not merely to exist; it initializes the object's attributes to sensible starting values. A well-initialized object therefore begins in a valid and predictable state.

New objectnot initialized__init__assigns attributesValid statepredictable valuesentersestablishes
What changes inside a newly created object when __init__ assigns its instance attributes?
Best practice

Always initialize every attribute that the object will use in __init__. This gives each instance a complete starting setup and helps ensure that its state is valid and predictable from the beginning of its lifecycle.

Class and Instance Roles

The class and its instances have different roles. The class is the source from which objects are created. Each instance is a particular object created from that class, with its own separate set of attributes initialized by its own call to __init__. The class therefore supplies the common structure for creation, while initialization gives each resulting object its independent state.

Classsource for instancesInstance Aseparate attributesInstance Bseparate attributescreatescreates
What does the class provide, and what does each individual instance contain?
ClassInstance
Provides the basis for creating objectsIs one particular object created from the class
Has the __init__ method that is automatically called during instance creationReceives its own initialization through a call to __init__
Can produce multiple instancesHas its own separate set of attributes

The source pack emphasizes the relationship between a class and its instances and the independent initialization of each instance.

Common Initialization Mistakes

  • Common mistake

    Treating __init__ as a method that must be called separately after every object is created

    What it looks like

    A learner creates an instance and assumes its attributes are uninitialized until they manually call __init__.

    Why it fails

    __init__ is automatically called whenever a new instance is created.

    Do this instead

    Trace instantiation first, then place the automatic __init__ call immediately after the new instance is created.

  • Common mistake

    Forgetting that self identifies the particular instance

    What it looks like

    A learner thinks one call to __init__ updates every object created from the class.

    Why it fails

    self refers to the specific object being created, so each instance is initialized independently.

    Do this instead

    Ask which object self refers to during each individual call to __init__.

  • Common mistake

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

    What it looks like

    An object is expected to use a status attribute, but that attribute is not initialized when the object begins its lifecycle.

    Why it fails

    The object may not start in a valid, predictable state.

    Do this instead

    Initialize all attributes the object will use in __init__.

  • Common mistake

    Confusing the class with one of its instances

    What it looks like

    A learner describes the class itself as though it were the individual object receiving a separate set of initialized attributes.

    Why it fails

    The class is the source for creating instances, while each instance has its own separate attributes.

    Do this instead

    Use the class as the creation source and identify the individual instance whose state is being initialized.

Trace the Mechanism

MEDIUM

A class is used to create two objects: Object One and Object Two. Each object is created at a different time, and the class has an __init__ method that assigns the object's starting attributes. Describe the execution flow in order, including what self refers to during the first call and during the second call.

Hints
  • Begin with the class as the source for creating instances.
  • Place an automatic __init__ call after each object is created.
  • Remember that self refers to a different specific object in each call.
Worked example

Tracing Two Object Creations

Explain what happens when a class creates Object One and then Object Two, assuming both objects use the class's __init__ method.

  1. 1
    Start with the class

    The class is available as the basis for creating objects.

  2. 2
    Create Object One

    A new instance is created, and Python automatically calls __init__ for it.

  3. 3
    Bind self for Object One

    During the first call, self refers to Object One, so the attributes initialized through self belong to Object One.

  4. 4
    Create Object Two

    A second new instance is created, and Python automatically calls __init__ again.

  5. 5
    Bind self for Object Two

    During the second call, self refers to Object Two, so its attributes are initialized separately from Object One's attributes.

Result

Both objects follow the same initialization process, but each receives its own initial state because each call to __init__ uses self for a different instance.

Key Takeaways

  1. 1
    __init__ is a special method that runs automatically when a new instance is created.
  2. 2
    This automatic call marks the beginning of the object's lifecycle.
  3. 3
    self refers to the specific object being created, so its attributes can be initialized independently.
  4. 4
    Every instance has its own separate set of attributes initialized by its own call to __init__.
  5. 5
    Initialize every attribute the object will use in __init__ so each instance starts in a valid, predictable state.

At a glance

  • __init__ runs automatically during the creation of every new instance.
  • The method establishes the object's initial state by initializing its attributes.
  • self connects the method to the particular instance currently being created.
  • Instances created from one class are initialized separately and have independent sets of attributes.
  • A reliable __init__ initializes all attributes that the object will use.

Related concepts

More concepts starting with I