Python Design Patterns

Chain of Responsibility Pattern in Python

Pass a request along a chain of handlers until one of them deals with it, like expense approvals by amount.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Linking handlers
  • Handling or passing on a request
  • Changing the chain order
approve(500)
  1. 1Approver("Lead", 100)limit 100: pass on
  2. 2Approver("Manager", 1000)limit 1000: Manager approved 500
  3. 3next = Nonewould return "Declined"

Pass it on until someone handles it

The problem

A request may need to be handled by one of several people or rules, depending on its size or type: small expenses by a team lead, larger ones by a manager. Hard-coding who handles what in the sender makes the rules rigid.

Think of it like this

Chain of Responsibility is like an expense claim going up the office. The team lead signs anything up to 100; a bigger claim is handed to the manager, who signs up to 1000. The employee only ever hands the claim to the lead and does not need to know who finally signs it.

From an expense claim to code
Approver("Lead", 100, ...)🧑 The team lead: signs up to 100
if amount <= self.limit:✅ Is it within my limit?
self.next.approve(amount)⬆️ No: hand it to the next person
"Manager approved 500"🧑‍💼 The manager signs it
Each handler either deals with the request or passes it on.
In one sentence

The Chain of Responsibility pattern passes a request along a chain of handlers until one of them handles it, so the sender does not need to know which handler will respond.

Key points

  1. Each Approver has a limit and an optional next approver.
  2. approve handles the amount if it is within self.limit.
  3. Otherwise it calls self.next.approve(amount), passing the request on.
  4. 500 is above the Lead's limit of 100, so the Manager approves it; above 1000 the result would be "Declined".

When should you use it?

  • Approval workflows with increasing limits.
  • Support escalation and request routing.
  • Middleware in web frameworks, where each layer can handle or pass on a request.

When not to use it

  • Exactly one handler always applies: a lookup is simpler.
  • Requests that must be handled by every step: that is a pipeline, not a chain.

Example code

Run code →
main.py
class Approver:
    # Chain of Responsibility: offer a request to handlers in order.
    def __init__(self, name, limit, next_approver=None):
        self.name, self.limit, self.next = name, limit, next_approver

    def approve(self, amount):
        # Handle locally or pass the request along the chain.
        if amount <= self.limit:
            return f"{self.name} approved {amount}"
        if self.next:
            return self.next.approve(amount)
        return "Declined"


chain = Approver("Lead", 100, Approver("Manager", 1000))
print(chain.approve(500))
Get a Run button for this example

Result of running the code

Manager approved 500

Code explanation

The chain is built by passing the Manager approver as the next approver of the Lead. approve(500) first runs on the Lead, whose limit of 100 is too low, so it forwards the request to self.next. The Manager's limit of 1000 covers 500, so it returns Manager approved 500.

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

Forgetting the end of the chain

Without a check, the last handler calls None.approve and crashes.

Problem
return self.next.approve(amount)
Fix
if self.next:
    return self.next.approve(amount)
return "Declined"

Finished this lesson?

Mark it as learned to track your progress.