Lesson 19 of 21 · 54 min · UG / professional
Classes, instances and methods
A class is a named record plus behaviour. __init__ fills fields. A method is a function that belongs with the data. Fifty motors should not be v1, a1, v2, a2.
Lab motor
Group fields; pass the whole record.
You will be able to
- ✓ See a class as a named record plus behaviour
- ✓ Treat __init__ as the constructor — here a factory function that returns a dict
- ✓ Keep methods as functions that take the record (self on a laptop)
A lab motor is not seven loose names
A motor has volts, amps and a power figure. Fifty motors should not be v1, a1, v2, a2. On a laptop you write class Motor: with __init__ to fill fields and methods to compute watts. This sandbox does not run class. We keep the dict as the runnable record and a factory motor(volts, amps) as the ancestor of __init__. Callers never remember whether the key is "volts" or "V".
An instance is one motor built from the class (or from the factory). A method is a function that belongs with the data: watts(m) here, m.watts() on a laptop. Construction in one place means the rest of the lab bench only calls motor(...).
Laptop: class, __init__, method
class Motor:
def __init__(self, volts, amps):
self.volts = volts
self.amps = amps
def watts(self):
return self.volts * self.amps
m = Motor(12, 2)
print(m.watts())
Behaviour lives next to the data — not next to HTTP
watts should return a number from the fields. Do not put file writes inside watts. Power is physics; writing a CSV is an edge. Objects go wrong when every method becomes a small program. Fifty instances in a list is the point of a class; fifty copy-pasted helpers is the failure.
Words that matter
- Class
- A named type for records plus functions that belong with them.
- Instance
- One motor, one student — a value built from the class.
- __init__
- Laptop constructor — fills fields when you call Motor(12, 2).
- Method
- A function stored on the class, called on an instance.
Common mistakes
Avoid: Fifty motors as v1, a1, v2, a2, ...
Do this: A list of motor records from one constructor.
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 constructor-shaped function
Callers never type the keys. Keys stay stable.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — behaviour as a function on the record
This is a method waiting for class syntax. 12 * 2 = 24.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — class Motor
One type, many instances. __init__ fills fields. Methods use self.
stdlib — no pip
Two motors, one formula.
Real library code (not run in this browser sandbox)
class Motor:
def __init__(self, volts, amps):
self.volts = volts
self.amps = amps
def watts(self):
return self.volts * self.amps
bench = [Motor(12, 2), Motor(24, 1)]
for m in bench:
print(m.watts())
Example program — Two motors, one formula
A list of records — fifty would look the same.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Student record
student(roll, name) returns a dict with both fields. Print student(19, "Ira") so both 19 and Ira appear.
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.