Concepts / The __init__ Constructor

The __init__ Constructor

A correct explanation should say that Notebook is the class blueprint, each Notebook() call creates an object instance, and two calls create two independent objects from the same class definition.

  • Programming

One Blueprint, Many Objects

When you work with classes, separate two ideas: the class and the object. A class is a reusable blueprint. An object is a concrete instance created from that blueprint. The __init__ constructor belongs to this object-creation context, so understanding the class-to-object relationship comes first.

Class definitionReusable blueprintObject 1Concrete instanceObject 2Concrete instancecreatescreates
How does one reusable class definition relate to multiple concrete objects?

The Smallest Class

The simplest class requires only four pieces: the class keyword, a name, a colon, and the pass statement. The pass statement supplies the class body when there is nothing else to place inside it.

python

From Class Name to Instance

Objects are created by calling the class name as a function. The result is an object instance of that class. A single class definition can be used to create multiple objects, and each object has its own memory location.

python
Worked example

Creating Two Instances

Use one class definition to create two object instances.

  1. 1
    Define the blueprint

    The Device class is defined once. Its class body uses pass because the example needs no additional class statements.

  2. 2
    Call the class name

    Device() calls the class name as a function and creates an object instance.

  3. 3
    Store the results

    The two calls produce first_device and second_device, which refer to two objects created from the same class definition.

Result

One class definition has produced two object instances. The instances share the same class definition but are independent objects with their own memory locations.

DeviceClassDevice()Class callObjectInstancecall the namecreates
How does control flow from a class name call to a newly created object instance?

Where __init__ Fits

The __init__ constructor is studied as part of creating objects from a class. The essential distinction is that the class is the reusable blueprint, while the object is the concrete instance produced from it. Calling the class name creates the object instance; the class definition remains reusable for creating additional instances.

Class nameReusable blueprintClass callDevice()__init__Constructor topicObject instanceConcrete objectcallobject-creation contextinstance result
What are the conceptual stages to keep in mind when a class call creates an object in the context of the __init__ constructor?

Class and Object Checks

ClassObject
Reusable blueprintConcrete instance
Defined with the class keyword, a name, a colon, and a bodyCreated by calling the class name as a function
Can be used to create multiple objectsEach object has its own memory location

The class describes the reusable source of instances; an object is one concrete result.

  • Common mistake

    Treating the class and the object as the same thing.

    What it looks like

    Thinking that Device and Device() name the same entity.

    Why it fails

    The class is the reusable blueprint, while Device() is a call that creates an object instance.

    Do this instead

    Use class language for the blueprint and object or instance language for the result of the class call.

  • Common mistake

    Assuming one class can create only one object.

    What it looks like

    Believing that creating first_device prevents creating second_device.

    Why it fails

    Multiple objects can be created from a single class.

    Do this instead

    Remember that the class definition is reusable.

  • Common mistake

    Leaving an empty class body without pass.

    What it looks like

    Writing a class header with no body statement.

    Why it fails

    The simplest class described in the source uses pass as its body.

    Do this instead

    Use pass when defining the simplest possible empty class.

Practice the Relationship

EASY

Consider this class definition: class Notebook: pass. Without adding any other class body statements, describe what Notebook represents and what Notebook() produces. Then explain what changes when Notebook() is called twice.

Hints
  • Identify the reusable blueprint first.
  • A class name followed by parentheses is a class call.
  • Compare the number of class definitions with the number of object instances.
Key point

A correct explanation should say that Notebook is the class blueprint, each Notebook() call creates an object instance, and two calls create two independent objects from the same class definition.

Essential Takeaways

  1. 1
    A class is a reusable blueprint.
  2. 2
    An object is a concrete instance created from a class.
  3. 3
    The simplest class uses the class keyword, a name, a colon, and pass.
  4. 4
    Calling a class name as a function creates an object instance.
  5. 5
    One class can create multiple independent objects, each with its own memory location.

At a glance

  • A class provides a reusable blueprint, while an object is one concrete instance of that blueprint.
  • The simplest possible class uses class, a class name, a colon, and pass.
  • Calling the class name as a function creates an object instance.
  • A single class can create multiple independent objects.
  • The __init__ constructor belongs to the object-creation context, but this source treatment focuses on the class-to-object relationship.

Related concepts

More concepts starting with T