What will be the effect of running the following code?
class A: def __init__(self,v): self.__a = v + 1 a = A(0) print(a.__a)
- 2
- The code will raise an AttributeError exception
- 0
- 1
Explanation: The following code will result in the AttributeError exception, because the A object does not have the attribute __a. The __a variable is a private instance variable, and an attempt to access or modify it from outside the class will result in an AttributeError.
Modifying the private attribute __a and changing it to public (a) will result in the following output: 1.
More Questions: Python Essentials 2 – Module 3 Test
Please login or Register to submit your answer