Defining Classes and Creating Objects
A class is a reusable blueprint; an object is a concrete instance created from that blueprint.
One Blueprint, Many Objects
A class gives Python a reusable blueprint. An object is a concrete instance created from that blueprint. The important relationship is that one class definition can serve as the basis for multiple objects, while each object remains an individual instance.
The Smallest Class Definition
The simplest possible class requires four parts: the class keyword, a name, a colon, and the pass statement. The pass statement supplies the required body when the class does not contain any other content.
This definition establishes Person as a class. It does not yet show a particular person object. The class is the reusable blueprint; an object appears when the class is called.
Calling the Class
Objects are created by calling the class name as a function. The parentheses after the class name are the operation that creates an object instance from the class definition.
class Person:
pass
person_a = Person()
person_b = Person()- 1Python reads the class definition and establishes Person as a class.
- 2
Person()creates an object instance, which is assigned to person_a. - 3Calling
Person()again creates another object instance, which is assigned to person_b.
- Person defines the reusable class.
- person_a receives one object created by calling Person.
- person_b receives another object created by calling Person.
Independent Instances
Creating Two Person Objects
Use one class definition to create two object instances.
- 1Define the class
Write one reusable Person class with the required class syntax and an empty body.
- 2Create the first object
Call
Person()and assign the resulting object to person_a. - 3Create the second object
Call
Person()again and assign the resulting object to person_b. - 4Compare the results
Both objects come from the same class definition, but each object gets its own memory location and is independent of the other.
One class definition has produced two independent object instances.
Class and Object Compared
| 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 | Is one individual result of calling the class |
The class describes the reusable source of objects; each object is an instance created from that source.
Mistakes with Classes
Common mistake Treating the class definition as though it were already an object.
What it looks likeclass Person: passWhy it failsThis code defines the reusable Person class, but it does not create a particular object instance.
Do this insteadCall the class to create an object: person_a = Person()
Common mistake Forgetting the parentheses when creating an object.
What it looks likeperson_a = PersonWhy it failsCalling the class name as a function is the operation described for creating an object.
Do this insteadUse person_a = Person()Common mistake Assuming that two objects created from one class are the same object.
What it looks likeperson_a = Person() person_b = Person()Why it failsMultiple objects can be created from one class, and each is independent with its own memory location.
Do this insteadTreat person_a and person_b as separate object instances that share the Person class definition.
Common mistake Leaving an empty class body without pass.
What it looks likeclass Person:Why it failsThe simplest class requires a body, and the pass statement provides the body when there is nothing else to place inside it.
Do this insteadWrite class Person: followed by an indented pass statement.
Check Your Understanding
Write a minimal empty class named Book. Then create two separate objects from it and assign them to book_a and book_b.
Hints
- An empty class needs the class keyword, a name, a colon, and pass.
- Create each object by writing the class name followed by parentheses.
What do you think happens?
Before checking the explanation, decide whether book_a and book_b should be treated as one object or as two independent objects.
Reveal answer
Two independent objects
A single class can create multiple objects. Each object is independent and gets its own memory location.
Key Takeaways
- 1A class is a reusable blueprint.
- 2An object is a concrete instance created from a class.
- 3The simplest class requires the class keyword, a name, a colon, and pass.
- 4Calling a class name with parentheses creates an object instance.
- 5One class can create multiple independent objects, and each object gets its own memory location.
At a glance
- A class is a reusable blueprint, while an object is a concrete instance of that blueprint.
- The simplest empty class uses class, a class name, a colon, and pass.
- Python creates an object when the class name is called with parentheses.
- A single class definition can create multiple independent objects.
- Each object created from the class has its own memory location.