Python, from Zero, for AILesson 62 of 89

Module 7 · Objects, generators and the shape a larger program takes

Inheritance and composition: swapping one model provider for another

In this lesson

  • 9 min read
  • 9 reading sections
  • Knowledge check
  • Lesson 3 of 10 in this chapter

This chapter’s goalRestructure a growing script into classes and dataclasses with a swappable provider behind one interface, implement the dunder methods that let your objects work with for, len, in and with, write a decorator that retries or caches, run a type checker and act on its output, make the project installable as a command with pyproject.toml, and profile it to say from measurement rather than guesswork where the time goes.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:40Not available
Full audiobookNot availableNot available
One interface, several providers, one object that holds oneYour programAsks one thing: assistant.say(question)AssistantHAS a provider and HAS a conversation: compositionChatProviderOne method: complete(messages, system) -> strAnthropicProvider · OpenAIProvider · OllamaProviderInherit the interface, absorb each service’s shapeFakeProviderReturns a canned string, so tests need no networkand no moneyInheritance is used once, for the interface, because every provider genuinely can be used wherever aChatProvider can. Everything else is composition: Assistant has a provider and has a conversation,which is what lets you swap a fake one in a test or fall back to a cheaper model when the budget isnearly spent.
Inheritance is used once, for the interface, because every provider genuinely can be used wherever a ChatProvider can. Everything else is composition: Assistant has a provider and has a conversation, which is what lets you swap a fake one in a test or fall back to a cheaper model when the budget is nearly spent.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 11Free · no account needed
Lesson steps

The problem it solves

Module 6 left you with a piece of advice: keep the model call in one function so that switching providers changes one place. Here is the shape that advice grows into when a program is large enough to need it.

Your program wants to say “ask the model this” and get text back. It does not want to know whether the model is Anthropic’s, OpenAI’s, or a free one running under Ollama. Those differ in URL, headers, request shape and response shape, and in nothing that the rest of the program cares about.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Inheritance and composition: swapping one model provider for another’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/inheritance-and-composition (Accessed: 21 November 2026).