What is the output of the following Python code snippet?
class A:
__slots__ = ('x',)
a = A()
try:
a.y = 10
except AttributeError:
print('AttributeError')
-
A
AttributeError
Correct answer
-
B
10
-
C
None
-
D
Error
Correct answer
A. AttributeError
5 views
Explanation
With __slots__, assigning an undeclared attribute raises AttributeError.
Doubts & discussion
Not sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ