ProgrammingModerateGATE CS & IT · Infosys

What is printed?

What is printed?
class S:
    __slots__ = ('a',)
s = S()
s.a = 1
try:
    s.b = 2
except AttributeError:
    print('no dict', s.a)
  1. A. no dict 2
  2. B. 2 1
  3. C. 1
  4. D. no dict 1

Correct answer: D. no dict 1

__slots__ removes the per-instance __dict__, so assigning a new attribute name raises AttributeError. The existing slot a keeps its value.

More Python Programming questions

Exams that ask this topic