Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.6 — Multiple Inheritance, Mixins and the MRO
Python · Example 2 of 2

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.

01

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.

02

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().

03

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

01

What is the MRO, and how do you see it?

02

What are the two rules of C3 linearisation?

03

What does super() really do in a multiple-inheritance hierarchy?

04

What is a mixin, and how should you design one?

Mixins for a tool call, and cooperative __init__
Sign in to run this example in your browser.

Expected output

ToolCall(name='search', query='python mro')
{"name": "search", "query": "python mro"}
['ToolCall', 'JsonMixin', 'ReprMixin', 'Base', 'object']
Asha 30