Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a property method named name.
Python
class Person: def __init__(self, name): self._name = name @[1] def name(self): return self._name
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @staticmethod or @classmethod instead of @property.
Forgetting the @ symbol before the decorator.
✗ Incorrect
The
@property decorator is used to define a method as a property, allowing access like an attribute.2fill in blank
mediumComplete the code to define a setter for the name property.
Python
class Person: def __init__(self, name): self._name = name @property def name(self): return self._name @name.[1] def name(self, value): self._name = value
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @name.getter instead of @name.setter.
Using @property again instead of @name.setter.
✗ Incorrect
The setter for a property is defined using the
@propertyname.setter decorator.3fill in blank
hardFix the error in the code by completing the property getter method correctly.
Python
class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self.[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning
self.radius causes infinite recursion.Using incorrect variable names like
Radius.✗ Incorrect
Inside the method, use the instance variable
_radius with self. prefix in the return statement to access it.4fill in blank
hardFill both blanks to create a property age with a getter and a deleter.
Python
class User: def __init__(self, age): self._age = age @[1] def age(self): return self._age @age.[2] def age(self): del self._age
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @age.setter instead of @age.deleter for deletion.
Forgetting to use @property for the getter.
✗ Incorrect
Use
@property to define the getter and @age.deleter to define the deleter for the property.5fill in blank
hardFill all three blanks to create a property score with getter, setter, and deleter methods.
Python
class Game: def __init__(self, score): self._score = score @[1] def score(self): return self._score @score.[2] def score(self, value): self._score = value @score.[3] def score(self): del self._score
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up setter and deleter decorators.
Forgetting to use @property for the getter.
✗ Incorrect
The property uses
@property for getter, @score.setter for setter, and @score.deleter for deleter.