Friday , September 20 2024

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)

Questions BankCategory: Python Essentials 2What 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)
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