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

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.

createstriggersinitializesClassdefinitionNew instanceobject__init__automatic callInitial stateattributes set
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. A class is available as the basis for creating instances.
  2. The program creates a new instance of that class.
  3. Python automatically calls the class's __init__ method.
  4. __init__ initializes the attributes belonging to that particular instance.
  5. The instance begins its lifecycle with an initial, predictable state.
makes possiblestartsbeginsClass definitionclass availableInstantiationnew object__init__automatic callObject lifecycleinitial state
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.

refers throughidentifies__init__initialization methodselfspecific objectInstance Aown attributes
How does self connect __init__ to the particular object being initialized?

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.

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.

Initialize the first instance: The attributes assigned through self become part of Amina's own initial state.

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.

Initialize the second instance: The attributes assigned through self become part of Ravi's own initial state, separate from Amina's attributes.

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.

entersestablishesNew objectnot initialized__init__assigns attributesValid statepredictable values
What changes inside a newly created object when __init__ assigns its instance attributes?

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.

createscreatesClasssource for instancesInstance Aseparate attributesInstance Bseparate attributes
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

Common Initialization Mistakes

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

    __init__ is automatically called whenever a new instance is created.

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

  • Forgetting that self identifies the particular instance

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

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

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

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

    Fix: Initialize all attributes the object will use in __init__.

  • Confusing the class with one of its instances

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

    Fix: 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.

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.

Start with the class: The class is available as the basis for creating objects.

Create Object One: A new instance is created, and Python automatically calls __init__ for it.

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

Create Object Two: A second new instance is created, and Python automatically calls __init__ again.

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.

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

Key Takeaways

  • __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.