Challenge - 5 Problems
Property Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of property getter
What is the output of this code?
Python
class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self._radius c = Circle(5) print(c.radius)
Attempts:
2 left
💡 Hint
Look at how the property decorator allows access to the private variable.
✗ Incorrect
The @property decorator makes the method 'radius' behave like an attribute. So, c.radius returns the value of self._radius, which is 5.
❓ Predict Output
intermediate2:00remaining
Output after setting property value
What will be the output of this code?
Python
class Person: def __init__(self, name): self._name = name @property def name(self): return self._name @name.setter def name(self, value): self._name = value p = Person('Alice') p.name = 'Bob' print(p.name)
Attempts:
2 left
💡 Hint
The setter changes the private variable _name.
✗ Incorrect
The setter method allows changing the value of _name through the property 'name'. So after setting p.name = 'Bob', printing p.name outputs 'Bob'.
❓ Predict Output
advanced2:00remaining
Output with property and deleter
What is the output of this code?
Python
class Data: def __init__(self): self._value = 10 @property def value(self): return self._value @value.deleter def value(self): del self._value d = Data() print(d.value) del d.value print(hasattr(d, '_value'))
Attempts:
2 left
💡 Hint
Deleting the property deletes the underlying attribute.
✗ Incorrect
The deleter removes the attribute _value. After deletion, hasattr(d, '_value') returns False. The first print shows 10 before deletion.
❓ Predict Output
advanced2:00remaining
Error when setting read-only property
What error does this code raise?
Python
class ReadOnly: @property def data(self): return 42 r = ReadOnly() r.data = 10
Attempts:
2 left
💡 Hint
Properties without a setter are read-only.
✗ Incorrect
Since 'data' has no setter, trying to assign to it raises AttributeError.
🧠 Conceptual
expert2:00remaining
Why use property decorators?
Which of the following is the main advantage of using property decorators in Python classes?
Attempts:
2 left
💡 Hint
Think about how properties help with encapsulation and attribute access.
✗ Incorrect
Property decorators let you define methods that act like attributes, so you can control getting, setting, and deleting private variables while keeping a simple interface.