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.
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.
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.
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.
Creating Two Instances
Use one class definition to create two object instances.
- 1Define the blueprint
The Device class is defined once. Its class body uses pass because the example needs no additional class statements.
- 2Call the class name
Device()calls the class name as a function and creates an object instance. - 3Store the results
The two calls produce
first_deviceandsecond_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.
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 and Object Checks
| Class | Object |
|---|---|
| Reusable blueprint | Concrete instance |
| Defined with the class keyword, a name, a colon, and a body | Created by calling the class name as a function |
| Can be used to create multiple objects | Each 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 likeThinking that Device and Device() name the same entity.
Why it failsThe class is the reusable blueprint, while
Device()is a call that creates an object instance.Do this insteadUse 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 likeBelieving that creating first_device prevents creating second_device.
Why it failsMultiple objects can be created from a single class.
Do this insteadRemember that the class definition is reusable.
Common mistake Leaving an empty class body without pass.
What it looks likeWriting a class header with no body statement.
Why it failsThe simplest class described in the source uses pass as its body.
Do this insteadUse pass when defining the simplest possible empty class.
Practice the Relationship
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
- 1A class is a reusable blueprint.
- 2An object is a concrete instance created from a class.
- 3The simplest class uses the class keyword, a name, a colon, and pass.
- 4Calling a class name as a function creates an object instance.
- 5One 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.