Python Design Patterns

State Pattern in Python

Let an object change its behavior when its state changes, like a media player that is playing or paused.

  • Advanced
  • 3 min read
  • 1 runnable example
What you'll learn
  • State objects instead of if chains
  • Transitions between states
  • Delegating behavior to the current state
player.action()
  1. 1Pausedaction() → Paused
  2. 2Playingaction() → Playing
↺ set_state() swaps the state object

The current state decides

The problem

A media player behaves differently when it is playing, paused or stopped. Writing one big method full of if state == ... checks makes every new state touch every method. The State pattern gives each state its own object.

Think of it like this

The State pattern is like the single play/pause button on a music player. The same press does different things depending on what the player is doing right now: it pauses when playing and plays when paused. Changing the state swaps what the button does, without rebuilding the player.

From a play/pause button to code
Paused(), Playing()⏯️ The two things the player can be doing
self.state💡 What it is doing right now
player.set_state(Playing())🔁 Switch to the other state
self.state.action()👆 The button asks the current state what to do
The current state object decides how the player behaves.
In one sentence

The State pattern lets an object change its behavior when its internal state changes, by delegating to a separate object for each state.

Key points

  1. Playing and Paused both have an action() method.
  2. Player starts in the Paused state.
  3. set_state replaces the current state object.
  4. Player.action() delegates to self.state.action(), so the result changes with the state.

When should you use it?

  • Objects with clear modes: media players, orders, connections, game characters.
  • Replacing large if / elif chains on a status field.
  • Workflows where each step allows different actions.

When not to use it

  • Two simple states with one small difference: a boolean flag is enough.
  • States that share almost all behavior: separate classes duplicate code.

Example code

Run code →
main.py
class Playing:
    # State: delegate behavior to the current state object.
    def action(self): return "Playing"


class Paused:
    def action(self): return "Paused"


class Player:
    def __init__(self): self.state = Paused()
    def set_state(self, state): self.state = state

    def action(self):
        # Behavior comes from the current state object.
        return self.state.action()


player = Player()
player.set_state(Playing())
print(player.action())
player.set_state(Paused())
print(player.action())
Get a Run button for this example

Result of running the code

Playing
Paused

Code explanation

Player keeps a state object instead of a status string. Its action() method simply asks the current state object to act. After set_state(Playing()) the call returns Playing, and after set_state(Paused()) it returns Paused, without any if statements in Player.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Checking the state with if statements

Every new state requires editing every method.

Problem
def action(self):
    if self.state == "playing":
        return "Playing"
    return "Paused"
Fix
def action(self):
    return self.state.action()

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges