Setter methods that check new values

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Setter methods that check new values

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which setter protects the private attribute _balance from a negative value?
A def set_balance(self, amount): self._balance = amount
B def set_balance(self, amount): if amount >= 0: self._balance = amount
2. What is the main job of a setter method that checks values?
A Change a private attribute only if the new value is valid
B Change a private attribute to whatever value it is given
3. Which line inside set_balance correctly rejects a bad value?
A if amount < 0: self._balance = amount
B if amount < 0: print("Invalid amount")
4. Account is an object with a private _balance. Which code uses the setter correctly from outside the class?
A acc.set_balance(150)
B acc.set_balance = 150
5. Which pair is a getter and setter for _balance?
A get_balance(self) returns self._balance; set_balance(self, amount) changes it
B get_balance(self) changes self._balance; set_balance(self, amount) returns it

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def set_balance(self, amount): ADefines a setter method that receives a new value for the private balance attribute so it can be checked before it is stored
2if amount >= 0: self._balance = amount Bdef get_balance(self): return self._balance
3A setter can reject a bad value by refusing to change the attribute and giving feedback instead CChecks the new value is valid and only then updates the private attribute
4A getter and setter pair lets code read a private attribute and change it safely Delse: print("Invalid amount")
5def set_balance(self, amount): self._balance = amount EThe value is saved to a local variable called balance, not to self._balance, so the object's attribute never changes
6def set_balance(self, amount): if amount >= 0: balance = amount FThe setter stores the value without checking it, so a negative amount could be saved and the object would hold bad data

Definitions

def set_balance(self, amount):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def ___________(self, amount): if amount >= 0: self._balance = amount
2.
__ amount >= 0: self._balance = amount
3.
if amount _ 0: return
4.
def ___________(self): return self._balance
5.
self.________ = amount

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def set_balance(self, amount):
B if amount >= 0:
C self.balance = amount
D else:
E print('Invalid')
Question 2
A def get_balance(self):
B return _balance
C def set_balance(self, amount):
D if amount > 0:
E self._balance = amount
Question 3
A def set_balance(self, amount):
B if amount >= 0:
C amount = self._balance
Question 4
A def set_balance(amount):
B if amount < 0:
C print('Bad amount')
D else:
E self._balance = amount
Question 5
A def set_balance(self amount):
B if amount >= 0:
C self._balance = amount
D return True
E return False

Answer Key

Setter methods that check new values
Section A: Quick Questions
Q1
B — def set_balance(self, amount): if amount >= 0: self._balance = amount
Q2
A — Change a private attribute only if the new value is valid
Q3
B — if amount < 0: print("Invalid amount")
Q4
A — acc.set_balance(150)
Q5
A — get_balance(self) returns self._balance; set_balance(self, amount) changes it
Section B: Match the Pairs
Q1
def set_balance(self, amount): → Defines a setter method that receives a new value for the private balance attribute so it can be checked before it is stored
Q2
if amount >= 0: self._balance = amount → Checks the new value is valid and only then updates the private attribute
Q3
A setter can reject a bad value by refusing to change the attribute and giving feedback instead → else: print("Invalid amount")
Q4
A getter and setter pair lets code read a private attribute and change it safely → def get_balance(self): return self._balance
Q5
def set_balance(self, amount): self._balance = amount → The setter stores the value without checking it, so a negative amount could be saved and the object would hold bad data
Q6
def set_balance(self, amount): if amount >= 0: balance = amount → The value is saved to a local variable called balance, not to self._balance, so the object's attribute never changes
Section C: Definitions
Q1
def set_balance(self, amount): — 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.
Section D: Fill in the Blanks
Q1
def set_balance(self, amount): if amount >= 0: self._balance = amount
Q2
if amount >= 0: self._balance = amount
Q3
if amount < 0: return
Q4
def get_balance(self): return self._balance
Q5
self._balance = amount
Section E: Spot the Error
Q1
C — 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.
Q2
B — 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.
Q3
C — The assignment is the wrong way round. The valid new value must be stored in the attribute: self._balance = amount.
Q4
A — Every method in a class needs self as its first parameter. Without it the setter cannot reach self._balance. It should be def set_balance(self, amount):.
Q5
A — Parameters must be separated by a comma. It should be def set_balance(self, amount):.