Setter methods that check new values
A setter method that changes a private attribute only after checking the new value is valid, such as rejecting a negative amount, so the object never holds bad data.
Understanding Setter methods that check new values in VCE Applied Computing U1O2
What it does: The else branch runs when validation fails, so the balance stays unchanged and the user is told the value was rejected. The getter returns the private value for reading, and it works alongside set_balance, which controls how the value is changed. A validating setter checks the new value with an if statement before it changes the private attribute. The second version accepts any value, even a bad one. A validating setter rejects -50, so the object keeps its old, valid balance. Putting the assignment inside the if block means it only runs for valid values. Outside the block, it runs every time and the check is useless.
Common mistakes: The setter must update the private attribute self._balance. Writing to self.balance creates a different public attribute, so the checked value is never stored in the right place. The getter must use self to reach the attribute. It should be return self._balance, because _balance on its own is not a defined variable.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 8–88) and word search (Modules 8–124).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
def set_balance(self, amount):Fill in the Blanks
Spot the Error
Student with a class code? Join your class →