Lesson 12 of 20 · 50 min · UG / professional
Operator overloading without two formulas
__add__ and __str__ are laptop syntax. The kiosk and the chart must still call one tested watts() or add_gst(). Overload by delegating to that function.
Lab motor
Group fields; pass the whole record.
You will be able to
- ✓ Put special behaviour in one named formula every caller uses
- ✓ Keep a kiosk and a chart from disagreeing on watts or GST
- ✓ Know operator overloading is this idea with extra syntax on a class
No magic, one formula
Full Python lets an object pretend to add (via __add__) or print itself (via __str__). You do not need that syntax on day one. You do need a named function that returns the same figure every caller uses — so a kiosk and a chart cannot disagree. watts(motor) is the professional habit. motor + motor as addition of two lab benches is cute and usually wrong.
When you later write those special methods, they should call the same function the tests already cover. Overloading is not a second copy of the physics or the GST slab.
Readable beats clever
print(motor) showing 12V 2A is useful. print(motor) firing a UPI request is a trap. Special behaviour should be boring: display, compare equality of ids, maybe add paise to paise. If you cannot explain the operator to a clerk, it should not exist.
Copy the class sample onto a laptop. This Run box has no class — only the formula both UIs must call.
Laptop only — __add__ delegates to one paise formula
def add_paise(a, b):
return a + b
class Paise:
def __init__(self, n):
self.n = n
def __add__(self, other):
return Paise(add_paise(self.n, other.n))
def __str__(self):
return str(self.n)
canteen = Paise(12000)
stores = Paise(8000)
print(canteen + stores) # same as add_paise(12000, 8000)
Words that matter
- Operator overloading
- Defining + or print for your type — laptop syntax on a class.
- Single formula
- One function both UI and tests call.
- __add__ / __str__
- Laptop hooks that must delegate, not copy the maths.
Common mistakes
Avoid: A kiosk that multiplies volts*amps inline and a chart that uses a different rounding.
Do this: watts(m) (or add_paise) in one place; __add__ only calls it.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — one watts function
Change the formula once. Two prints follow.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — UPI add as one formula
Paise integers. Same function a later __add__ would call.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Equality by id, not by accident
Two records with the same id are the same device for this policy.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Shared watts
watts(v, a) returns v * a. Print watts(12, 2) so 24 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.