State Pattern in Python
Let an object change its behavior when its state changes, like a media player that is playing or paused.
- State objects instead of if chains
- Transitions between states
- Delegating behavior to the current state
- 1
Pausedaction() → Paused - 2
Playingaction() → Playing
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.
Paused(), Playing()⏯️ The two things the player can be doingself.state💡 What it is doing right nowplayer.set_state(Playing())🔁 Switch to the other stateself.state.action()👆 The button asks the current state what to doThe State pattern lets an object change its behavior when its internal state changes, by delegating to a separate object for each state.
Key points
PlayingandPausedboth have anaction()method.Playerstarts in thePausedstate.set_statereplaces the current state object.Player.action()delegates toself.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/elifchains 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
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())
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.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Checking the state with if statements
Every new state requires editing every method.
def action(self):
if self.state == "playing":
return "Playing"
return "Paused"
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