Lesson 42 of 55
9 mins readPython Multiple Inheritance & Method Search
In Plain English
Multiple inheritance is implemented by setting `__index` to a function that searches across multiple parent class tables sequentially until the requested method is found.
Deep Dive: How It Works
__index as Search Function: `function(table, key)` iterates parent classes.
Mixin Pattern: Composing behavior from multiple source tables into a new class.
Search Order: Determines method resolution order (MRO) when names collide.
Core Rules to Remember

Custom __index Search: Searches an array of parent classes dynamically.

Composition & Mixins: Combines capabilities from multiple classes (e.g. CanFly and CanSwim).
Live Interactive Example
Hit Run Code to see it liveMultiple Inheritance via __index Search Function
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Output Console
Click "Run Code" to view the rendered output.
How it works: __index searched Walker first, then Flyer, finding both methods.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Combine Two Mixins
Create `local Loggable = { log = function(self, msg) return "[LOG] " .. msg end }`.
Create `local User = { name = "Alice" }`. Set metatable `{ __index = Loggable }`.
Print(User:log("User created")).
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.