0
0
Pythonprogramming~10 mins

Property decorator usage in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aclassmethod
Bstaticmethod
Cproperty
Dabstractmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using @staticmethod or @classmethod instead of @property.
Forgetting the @ symbol before the decorator.
2fill in blank
medium

Complete 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'
Agetter
Bproperty
Cdeleter
Dsetter
Attempts:
3 left
💡 Hint
Common Mistakes
Using @name.getter instead of @name.setter.
Using @property again instead of @name.setter.
3fill in blank
hard

Fix 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'
Aself._radius
Bradius
CRadius
D_radius
Attempts:
3 left
💡 Hint
Common Mistakes
Returning self.radius causes infinite recursion.
Using incorrect variable names like Radius.
4fill in blank
hard

Fill 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'
Aproperty
Bsetter
Cdeleter
Dgetter
Attempts:
3 left
💡 Hint
Common Mistakes
Using @age.setter instead of @age.deleter for deletion.
Forgetting to use @property for the getter.
5fill in blank
hard

Fill 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'
Aproperty
Bsetter
Cdeleter
Dgetter
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up setter and deleter decorators.
Forgetting to use @property for the getter.