0
0
Pythonprogramming~20 mins

Getter and setter methods in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Getter and Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ATypeError
BNone
CAttributeError
DAlice
Attempts:
2 left
💡 Hint
Look at how the @property decorator allows access to the private variable.
Predict Output
intermediate
2: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)
A30
B25
CValueError
DAttributeError
Attempts:
2 left
💡 Hint
The setter updates the private variable if the value is valid.
Predict Output
advanced
2: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
ANo error
BTypeError
CValueError
DAttributeError
Attempts:
2 left
💡 Hint
Check the condition inside the setter method.
🧠 Conceptual
advanced
2:00remaining
Why use getter and setter methods?
Which of these is the main reason to use getter and setter methods in Python classes?
ATo control access and validation when getting or setting an attribute
BTo make code run faster
CTo avoid using classes altogether
DTo automatically print attribute values
Attempts:
2 left
💡 Hint
Think about why you might want to check or change a value before saving it.
Predict Output
expert
2: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)
A
100
-50
B
100
Cannot set negative balance
100
C
Cannot set negative balance
-50
D
100
100
Attempts:
2 left
💡 Hint
Notice what happens when trying to set a negative balance.