ProgrammingVery hardProduct company OA

What is printed?

What is printed?
class Acc:
    def __init__(self):
        self.items = []
    def __iadd__(self, v):
        self.items.append(v)

a = Acc()
b = a
a += 1
print(a, len(b.items), b is a)
  1. A. None 1 False
  2. B. <Acc> 1 True
  3. C. None 0 False
  4. D. None 1 True

Correct answer: A. None 1 False

a += 1 calls a.__iadd__(1) and then ASSIGNS its return value back to a. The method mutates the object (items becomes [1]) but forgets to return self, so it returns None and a is rebound to None. b still refers to the original object, which holds one item, and b is a is now False.

More Python Programming questions

Exams that ask this topic