Concepts / Defining Classes and Creating Objects

Defining Classes and Creating Objects

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

  • Programming

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.

createscreatesPersonclass definitionperson_aobject instanceperson_bobject instance
How does one class definition serve as a blueprint for multiple concrete objects?

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.

python

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()

followed bycreatesPersonclass nameperson_aobject instance()call
What happens when Python executes a class name followed by parentheses?

Independent Instances

Creating Two Person Objects

Use one class definition to create two object instances.

Define the class: Write one reusable Person class with the required class syntax and an empty body.

Create the first object: Call Person() and assign the resulting object to person_a.

Create the second object: Call Person() again and assign the resulting object to person_b.

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

python

Class and Object Compared

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 objectsIs one individual result of calling the class
createsPersonreusable blueprintperson_aconcrete instance
What is the difference between the reusable class blueprint and each individual object instance?

Mistakes with Classes

  • Treating the class definition as though it were already an object.

    This code defines the reusable Person class, but it does not create a particular object instance.

    Fix: Call the class to create an object: person_a = Person()

  • Forgetting the parentheses when creating an object.

    Calling the class name as a function is the operation described for creating an object.

    Fix: Use person_a = Person()

  • Assuming that two objects created from one class are the same object.

    Multiple objects can be created from one class, and each is independent with its own memory location.

    Fix: Treat person_a and person_b as separate object instances that share the Person class definition.

  • Leaving an empty class body without pass.

    The simplest class requires a body, and the pass statement provides the body when there is nothing else to place inside it.

    Fix: Write class Person: followed by an indented pass statement.

Check Your Understanding

EASY

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.

  • One object
  • Two independent objects
Reveal answer

Answer: Two independent objects

A single class can create multiple objects. Each object is independent and gets its own memory location.

Key Takeaways

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

Key Takeaways

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