Python Design Patterns

Adapter Pattern in Python

Make an old or third-party class fit the interface your code expects, without changing that class.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Incompatible interfaces
  • Wrapping the old object
  • Translating calls and data
pay(25)
  1. 1client.pay(25)The interface the client knows
  2. 2PaymentAdapterTranslates the call
  3. 3legacy.make_payment(...)The old interface
  4. 4PAY 25.00Result

A translator between two interfaces

The problem

You often have to use code you cannot change, such as an old payment system or a third-party library, whose methods do not match what the rest of your program expects. Rewriting every caller is risky. An adapter translates between the two.

Think of it like this

An adapter is like a travel plug adapter. Your laptop plug fits your home sockets, and the hotel's sockets are a different shape. The adapter sits between them, so neither the laptop nor the wall has to change.

From a travel plug to code
gateway.pay(25)🔌 Your plug: pay in dollars
PaymentAdapter🔁 The travel adapter in between
round(dollars * 100)💱 Convert dollars to cents
make_payment(cents)🧱 The old socket: wants cents
The adapter translates the call; neither side changes.
In one sentence

The Adapter pattern wraps an object with an incompatible interface in a class that offers the interface the client expects and translates each call.

Key points

  1. LegacyGateway only offers make_payment(cents).
  2. PaymentAdapter offers pay(dollars), the method the rest of the program wants to call.
  3. round(dollars * 100) converts dollars to cents before calling make_payment.
  4. gateway.pay(25) works without changing LegacyGateway at all.

When should you use it?

  • Using old or third-party code behind the interface your program expects.
  • Switching between providers, such as payment or email services, without changing callers.
  • Converting units or data formats at the boundary.

When not to use it

  • You own both sides and can simply rename or change one of them.
  • The interfaces differ so much that the adapter would contain most of the logic: redesign the boundary instead.

Example code

Run code →
main.py
class LegacyGateway:
    # Adapter: wrap an incompatible API with the expected interface.
    def make_payment(self, cents):
        return f"PAY {cents / 100:.2f}"


class PaymentAdapter:
    def __init__(self, gateway):
        self.gateway = gateway

    def pay(self, dollars):
        # Translate both the method name and amount format.
        return self.gateway.make_payment(round(dollars * 100))


gateway = PaymentAdapter(LegacyGateway())
print(gateway.pay(25))
Get a Run button for this example

Result of running the code

PAY 25.00

Code explanation

LegacyGateway expects amounts in cents through make_payment, while the rest of the program wants to call pay with dollars. PaymentAdapter stores the old gateway and implements pay by converting dollars to cents and calling make_payment. The caller uses pay(25), and the legacy class receives 2500 without being modified.

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 to convert the values

Renaming the method alone sends dollars where cents are expected.

Problem
def pay(self, dollars):
    return self.gateway.make_payment(dollars)
Fix
def pay(self, dollars):
    return self.gateway.make_payment(round(dollars * 100))

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