Unit 12 · lesson

Classes, Objects & Instances

Core path: 25 minutes

When does grouping data into an object make a program easier to reason about?

Start with repeated structure

Without objects, several players might be represented as dictionaries:

player1 = {"name": "Nova", "score": 850}
player2 = {"name": "Maya", "score": 950}

That works. Classes are not a magic upgrade. They become useful when a repeated kind of thing has related state and behavior that belong together.

Define a class

class Player:
    def __init__(self, name, score):
        self.name = name
        self.score = score

A class defines the structure and behavior for a kind of object. An instance is one actual object created from that class.

Interactive model

One class, independent instance state

The class defines the shared structure. Each constructor call creates a separate instance whose attributes can change independently.

Drag nodes to inspect the relationships. Motion shows the active path; the plain background keeps attention on the relationships instead of graph-paper decoration.

View static diagramStatic Python class to instances model

self refers to the current instance

nova = Player("Nova", 850)
maya = Player("Maya", 950)

Each call creates a different instance. self.name does not mean one global Player name. It means the name attribute belonging to whichever instance is currently being initialized or used.

Self Visualization
Self Visualization

Diagrams open at a readable shape-aware scale. Zoom or expand when you need more detail.

Guided example: prove the state is independent

Run:

nova = Player("Nova", 850)
maya = Player("Maya", 950)

nova.score = 900

print(nova.score)
print(maya.score)

Predict before running:

900
950

Why did Maya's score remain 950? Both objects follow the same class definition, but each instance carries its own state.

Now imagine a bug where changing one player unexpectedly changes every player. That failure would tell you the state is being shared somewhere you did not intend, which is a design problem worth tracing rather than blaming on "objects being weird."

Attributes and behavior

name and score are attributes. In the next lesson, methods will place behavior next to the state it is responsible for changing.

When not to use a class

If a program only needs one tiny record and no meaningful behavior, a dictionary or simple function may be clearer. Architecture is a tradeoff, not a contest to use the most advanced syntax.

Vocabulary lab

Flip the idea, not just the card

Explain the term before you reveal the back. Then compare your explanation with the definition, example, and warning.

1 / 5
Read all terms without animation
Class
A definition describing the state and behavior associated with a kind of object. Example: class Player defines what Player instances contain. Do not confuse it with: One specific Player instance such as nova.
Instance
A specific object created from a class definition. Example: nova = Player('Nova', 850) creates an instance. Do not confuse it with: The class definition shared by all Player objects.
Attribute
Data associated with a specific object. Example: nova.score is an attribute on the nova instance. Do not confuse it with: A standalone local variable with no object ownership.
State
The current data values that describe an object's condition at a point in time. Example: A player's current score is part of its state. Do not confuse it with: The class definition before any instance exists.
self
The conventional parameter that refers to the current instance inside an instance method. Example: self.score accesses the score of the current Player. Do not confuse it with: A global variable containing every object.