Lesson 15 of 20 · 52 min · UG / professional
staticmethod, classmethod, mix-ins, factories
A helper that needs no instance is static. A constructor alternative that needs the class is a classmethod. Mix-ins share a method across types. A factory returns the right record without five ifs at every call site.
Lab motor
Group fields; pass the whole record.
You will be able to
- ✓ See a helper with no instance as the analog of staticmethod
- ✓ See an alternate constructor as the analog of classmethod / a factory
- ✓ Share one labelled helper across two record kinds (mix-in analog)
Not every helper needs a row
GST 18 does not care which canteen bill you pass — that is a staticmethod on a laptop: a function parked on a type for namespacing. A classmethod is an alternate constructor that receives the class: Room.from_percent(used, seats) builds the record. A factory is the same idea without insisting on class syntax — make_room(kind, used, seats) returns the right dict.
This sandbox has no class. You write gst_rate() with no row, make_room(...) that returns a dict, and labelled(row) used by both a sensor and a hall record. That is static / class / factory / mix-in energy without the keywords.
Mix-ins share a method, not a family tree
Two types that both need a display line should call one labelled() rather than copy f-strings. On a laptop a mix-in class can donate that method. Inheritance is not required if a function will do.
Factories hide the if
if kind == "lab" at every call site will drift. One factory returns the record. Callers print fields. Tests lock the shape.
Words that matter
- staticmethod
- A helper that needs no instance — a plain function here.
- classmethod / factory
- An alternate constructor — make_room returns the right dict.
- Mix-in
- Shared behaviour donated to more than one kind of record.
Common mistakes
Avoid: Copying the same display f-string into sensor.py and hall.py.
Do this: One labelled() (mix-in analog) both records call.
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 — helper with no row (static analog)
GST rate does not need a bill object.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — factory returns the right record
Callers do not keep five ifs. Nested if, no and/or.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Mix-in analog: one labelled() for two kinds
Sensor and room share the helper. percent stays a separate formula.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Factory kind
make_room(kind) returns a dict with that kind. Print make_room("lab") so lab 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.