Recall & Review
beginner
What is a getter method in Python?
A getter method is a function that retrieves or returns the value of a private attribute in a class. It allows controlled access to the attribute.
Click to reveal answer
beginner
What is a setter method in Python?
A setter method is a function that sets or updates the value of a private attribute in a class. It allows controlled modification of the attribute.
Click to reveal answer
intermediate
Why use getter and setter methods instead of accessing attributes directly?
Getter and setter methods help protect data by controlling how attributes are accessed or changed. They can add checks or rules before allowing changes.
Click to reveal answer
intermediate
How do you define a getter method using @property in Python?
You define a method with the @property decorator above it. This method acts like an attribute when accessed, returning the value you want.
Click to reveal answer
intermediate
How do you define a setter method using @property_name.setter in Python?
You define a method with the @property_name.setter decorator, where property_name matches the getter. This method sets the value with any checks you want.
Click to reveal answer
What does a getter method do?
✗ Incorrect
A getter method returns the value of an attribute, allowing controlled access.
Which decorator is used to define a getter method in Python?
✗ Incorrect
The @property decorator is used to define a getter method.
How do you define a setter method for a property named 'age'?
✗ Incorrect
The setter method uses @age.setter where 'age' is the property name.
Why might you use a setter method instead of changing an attribute directly?
✗ Incorrect
Setter methods allow adding checks or validation before changing an attribute.
What happens if you try to set a property without defining a setter method?
✗ Incorrect
Without a setter, trying to set a property raises an AttributeError.
Explain how getter and setter methods help protect data in a Python class.
Think about how you can check or limit changes to data.
You got /4 concepts.
Describe how to create a property with both getter and setter methods using decorators.
Remember the naming must match between getter and setter.
You got /4 concepts.