Mixins for a tool call, and cooperative __init__
ToolCall is the kind of small record an AI agent passes around. Add a new mixin and list it in the bases.
A class can have several parents. Python decides which parent's method runs by following the method resolution order (MRO), computed by the C3 linearisation rule, and super() follows that same order. Mixins use this to add small, focused abilities to many classes.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write classes A, B(A), C(A) and D(B, C) each with name() returning its own letter. Print D().name() and the MRO names. Explain why.
Write a ShoutMixin with a method shout() that returns self.speak().upper() + "!", and a Dog class with speak(); combine them as LoudDog(ShoutMixin, Dog) and print LoudDog().shout().
Write a hierarchy where B and C both inherit A and each calls super().__init__(), appending its letter to a list stored in A, to show each runs once. Create D(B, C) and print the list.
Explain it without notes
What is the MRO, and how do you see it?
What are the two rules of C3 linearisation?
What does super() really do in a multiple-inheritance hierarchy?
What is a mixin, and how should you design one?
Expected output
ToolCall(name='search', query='python mro')
{"name": "search", "query": "python mro"}
['ToolCall', 'JsonMixin', 'ReprMixin', 'Base', 'object']
Asha 30