Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the @property decorator in Python?
The @property decorator allows you to define a method that can be accessed like an attribute. It helps to control access to instance variables while keeping the syntax simple and clean.
Click to reveal answer
intermediate
How do you define a setter method for a property in Python?
You define a setter method by using the @property_name.setter decorator above a method with the same name as the property. This method allows you to set the value while controlling or validating it.
Click to reveal answer
beginner
What happens if you try to set a value to a property without defining a setter?
If no setter is defined, trying to assign a value to the property will raise an AttributeError. This protects the property from being changed directly.
Click to reveal answer
beginner
Explain the difference between a regular method and a property method in Python.
A regular method requires parentheses to be called (e.g., obj.method()), while a property method can be accessed like an attribute without parentheses (e.g., obj.property). Properties provide a way to use methods as if they were attributes.
Click to reveal answer
beginner
Show a simple example of a class with a property and a setter using the <code>@property</code> decorator.
Example:<br><pre>class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value:
raise ValueError('Name cannot be empty')
self._name = value
p = Person('Alice')
print(p.name) # Outputs: Alice
p.name = 'Bob' # Sets new name
</pre>
Click to reveal answer
What does the @property decorator do in Python?
ATurns a method into a read-only attribute
BDefines a class method
CCreates a static method
DDeletes an attribute
✗ Incorrect
The @property decorator makes a method behave like a read-only attribute.
How do you define a setter for a property named age?
AUsing <code>@age.set</code>
BUsing <code>@property.setter</code>
CUsing <code>@set_age</code>
DUsing <code>@age.setter</code> above the setter method
✗ Incorrect
The setter for a property age is defined with @age.setter.
What error occurs if you try to set a property without a setter?
ANameError
BAttributeError
CValueError
DTypeError
✗ Incorrect
Trying to set a property without a setter raises an AttributeError.
Which of these is a benefit of using properties?
AControl access to attributes with simple syntax
BMake methods run faster
CAllow multiple inheritance
DAutomatically document code
✗ Incorrect
Properties let you control attribute access while keeping syntax clean.
How do you access a property method in Python?
AUsing square brackets
BWith parentheses like a normal method
CLike an attribute, without parentheses
DUsing the <code>get()</code> function
✗ Incorrect
Property methods are accessed like attributes, without parentheses.
Explain how the @property decorator works and why it is useful.
Think about how you can hide internal details but still let users get or set values easily.
You got /4 concepts.
Describe how to create a property with both getter and setter methods in a Python class.
Remember the setter decorator uses the property name.
You got /4 concepts.
Practice
(1/5)
1.
What does the @property decorator do in a Python class?
easy
A. It converts a function into a static method.
B. It makes a method private.
C. It allows a method to be accessed like an attribute.
D. It deletes an attribute from the class.
Solution
Step 1: Understand the role of @property
The @property decorator lets you call a method without parentheses, like an attribute.
Step 2: Compare options
Only It allows a method to be accessed like an attribute. correctly describes this behavior. Other options describe unrelated features.
Final Answer:
It allows a method to be accessed like an attribute. -> Option C
Quick Check:
@property makes method act like attribute [OK]
Hint: Remember: @property hides () making method look like attribute [OK]
Common Mistakes:
Thinking @property makes method private
Confusing @property with @staticmethod
Believing @property deletes attributes
2.
Which of the following is the correct syntax to define a setter for a property named value?
class MyClass:
@property
def value(self):
return self._value
# What goes here?
easy
A. @setter.value\ndef value(self, val):\n self._value = val
B. @value.setter\ndef value(self, val):\n self._value = val
C. @value.set\ndef set_value(self, val):\n self._value = val
D. @value.setter\ndef set_value(self, val):\n self._value = val
Solution
Step 1: Identify correct setter syntax
The setter uses the property name with @value.setter and defines a method with the same name value.
Step 2: Check method name and decorator
@value.setter\ndef value(self, val):\n self._value = val correctly uses @value.setter and method value. Others use wrong decorator or method names.
Final Answer:
@value.setter\ndef value(self, val):\n self._value = val -> Option B
Quick Check:
Setter uses @propertyname.setter and same method name [OK]
Hint: Setter decorator is @propertyname.setter with same method name [OK]
Common Mistakes:
Using @setter.value instead of @value.setter
Changing method name in setter
Using @value.set instead of @value.setter
3.
What will be the output of the following code?
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
self._radius = 0
else:
self._radius = value
c = Circle(5)
c.radius = -3
print(c.radius)
medium
A. 0
B. 5
C. -3
D. AttributeError
Solution
Step 1: Understand setter logic
When setting radius, if value < 0, it sets _radius to 0, else to value.
Step 2: Trace code execution
Initial radius is 5. Then c.radius = -3 triggers setter, sets _radius to 0. Printing c.radius returns 0.
Final Answer:
0 -> Option A
Quick Check:
Setter sets negative radius to 0 [OK]
Hint: Setter changes negative radius to zero, so output is 0 [OK]
Common Mistakes:
Expecting original value 5 to remain
Printing -3 instead of 0
Confusing property with direct attribute
4.
Find the error in this code using property decorators and fix it.
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def set_name(self, value):
self._name = value
p = Person('Alice')
p.name = 'Bob'
print(p.name)
medium
A. Change setter method name to name instead of set_name.
B. Remove the @property decorator.
C. Change self._name to self.name in setter.
D. Add a deleter method for name.
Solution
Step 1: Identify setter method name mismatch
The setter decorator @name.setter requires the method to be named name, but here it is set_name.
Step 2: Fix method name
Rename the setter method to name to match the property name and decorator.
Final Answer:
Change setter method name to name instead of set_name. -> Option A
Quick Check:
Setter method name must match property name [OK]
Hint: Setter method name must match property name exactly [OK]
Common Mistakes:
Using different method name for setter
Removing @property decorator mistakenly
Changing attribute name inside setter
5.
Consider a class that stores a temperature in Celsius internally but exposes it as Fahrenheit using property decorators. Which code correctly implements this?