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.

Personclass definitionperson_aobject instanceperson_bobject instancecreatescreates
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.

Code walkthrough
class Person:
    pass

person_a = Person()
person_b = Person()
  1. 1
    Python reads the class definition and establishes Person as a class.
  2. 2
    Person() creates an object instance, which is assigned to person_a.
  3. 3
    Calling 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.
Personclass nameperson_aobject instance()callfollowed bycreates
What happens when Python executes a class name followed by parentheses?

Independent Instances

Worked example

Creating Two Person Objects

Use one class definition to create two object instances.

  1. 1
    Define the class

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

  2. 2
    Create the first object

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

  3. 3
    Create the second object

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

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

Result

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

The class describes the reusable source of objects; each object is an instance created from that source.

Personreusable blueprintperson_aconcrete instancecreates
What is the difference between the reusable class blueprint and each individual object instance?

Mistakes with Classes

  • Common mistake

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

    What it looks like
    class Person:
        pass
    Why it fails

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

    Do this instead

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

  • Common mistake

    Forgetting the parentheses when creating an object.

    What it looks like
    person_a = Person
    Why it fails

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

    Do this instead
    Use person_a = Person()
  • Common mistake

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

    What it looks like
    person_a = Person()
    person_b = Person()
    Why it fails

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

    Do this instead

    Treat 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 like
    class Person:
    Why it fails

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

    Do this instead

    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. 1
    A class is a reusable blueprint.
  2. 2
    An object is a concrete instance created from a class.
  3. 3
    The simplest class requires the class keyword, a name, a colon, and pass.
  4. 4
    Calling a class name with parentheses creates an object instance.
  5. 5
    One 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.

Related concepts

More concepts starting with D