Lesson 10: Setters & validation
A property without a setter blocks every update — but sometimes an update is exactly what's needed, just safely. Today we add @balance.setter with validation: a valid value gets in, a bad one is stopped with raise. This is the encapsulation module's peak.
The setter is a bouncer at a club's door: every value that wants in gets checked — those that follow the rules enter, those that don't are stopped at the door with an explanation.
- setter
- A method marked @name.setter that runs automatically on every assignment to the property — the place to check the incoming value.
- validation
- Checking a value is legal before storing it — and rejecting it with raise when it isn't.