Challenge - 5 Problems
Getter and Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of getter method usage
What is the output of this Python code using a getter method?
Python
class Person: def __init__(self, name): self._name = name @property def name(self): return self._name p = Person("Alice") print(p.name)
Attempts:
2 left
💡 Hint
Look at how the @property decorator allows access to the private variable.
✗ Incorrect
The @property decorator makes the method 'name' act like an attribute. So, p.name returns the value of _name, which is 'Alice'.
❓ Predict Output
intermediate2:00remaining
Output after setting a value with setter
What will be printed after running this code that uses a setter method?
Python
class Temperature: def __init__(self, celsius): self._celsius = celsius @property def celsius(self): return self._celsius @celsius.setter def celsius(self, value): if value < -273.15: raise ValueError("Temperature below absolute zero!") self._celsius = value t = Temperature(25) t.celsius = 30 print(t.celsius)
Attempts:
2 left
💡 Hint
The setter updates the private variable if the value is valid.
✗ Incorrect
The setter method changes _celsius to 30 because 30 is above absolute zero. So, printing t.celsius shows 30.
❓ Predict Output
advanced2:00remaining
What error is raised when setting invalid value?
What error does this code raise when trying to set an invalid temperature?
Python
class Temperature: def __init__(self, celsius): self._celsius = celsius @property def celsius(self): return self._celsius @celsius.setter def celsius(self, value): if value < -273.15: raise ValueError("Temperature below absolute zero!") self._celsius = value t = Temperature(20) t.celsius = -300
Attempts:
2 left
💡 Hint
Check the condition inside the setter method.
✗ Incorrect
The setter raises a ValueError because -300 is less than -273.15, which is not allowed.
🧠 Conceptual
advanced2:00remaining
Why use getter and setter methods?
Which of these is the main reason to use getter and setter methods in Python classes?
Attempts:
2 left
💡 Hint
Think about why you might want to check or change a value before saving it.
✗ Incorrect
Getters and setters let you control how attributes are accessed or changed, such as adding checks or calculations.
❓ Predict Output
expert2:00remaining
Output of complex getter and setter interaction
What is the output of this code that uses both getter and setter with extra logic?
Python
class BankAccount: def __init__(self): self._balance = 0 @property def balance(self): return self._balance @balance.setter def balance(self, amount): if amount < 0: print("Cannot set negative balance") else: self._balance = amount account = BankAccount() account.balance = 100 print(account.balance) account.balance = -50 print(account.balance)
Attempts:
2 left
💡 Hint
Notice what happens when trying to set a negative balance.
✗ Incorrect
The setter prints a warning and does not change _balance if the amount is negative. So balance stays 100 after the failed set.