Lesson 37 of 55
9 mins readPython Prototype-Based OOP & Class Tables
In Plain English
Lua achieves object-oriented programming through prototype tables. A class is a table that serves as a prototype for instances using `setmetatable(instance, { __index = Class })`.
Deep Dive: How It Works
Prototype Pattern: Objects delegate missing method calls to their class table.
Class Table Setup: `Account = {}; Account.__index = Account`.
Instance Creation: `setmetatable(new_table, Account)` gives `new_table` access to all methods in `Account`.
Core Rules to Remember

Prototype OOP: Class methods are defined on a prototype table and inherited via `__index`.

Lightweight Objects: Instances are simple tables storing only their own field values.
Live Interactive Example
Hit Run Code to see it liveSimple Prototype Class in Lua
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: g1 inherited say_hi from the Greeter class prototype.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Create Point Prototype Class
Define `local Point = {} Point.__index = Point`.
Add `function Point.get_x(self) return self.x end`.
Create `local p = { x = 42 }` with metatable `Point` and print `"X: " .. p:get_x()`.
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.