Concepts / The __init__ Constructor

The __init__ Constructor

A class is a reusable blueprint; an object is a concrete instance created from that blueprint.

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

createscreatesClass definitionReusable blueprintObject 1Concrete instanceObject 2Concrete instance
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

Creating Two Instances

Use one class definition to create two object instances.

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

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

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

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

call the namecreatesDeviceClassDevice()Class callObjectInstance
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.

callobject-creation contextinstance resultClass nameReusable blueprintClass callDevice()__init__Constructor topicObject instanceConcrete object
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
  • Treating the class and the object as the same thing.

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

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

  • Assuming one class can create only one object.

    Multiple objects can be created from a single class.

    Fix: Remember that the class definition is reusable.

  • Leaving an empty class body without pass.

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

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

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

Key Takeaways

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