Classes & Objects · Question 19 · Hard

total is a property with only a getter. What happens when the code tries to assign to it?

class Order:
    def __init__(self):
        self._total = 100
    @property
    def total(self):
        return self._total

o = Order()
o.total = 200
print(o.total)
  1. 200
  2. 100
  3. AttributeError
  4. TypeError
Answer and explanation

Answer: C. AttributeError

Explanation: A property without a setter is read-only. The assignment o.total = 200 raises AttributeError before the print is reached.

Previous: The Coin class defines both __str__ and __repr__. What is printed for the… · Next: sell() uses self.stock -= 1 where stock starts as a class attribute, and the…

Practise classes & objects

Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.

More classes & objects questions

More python practice

Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge