class Demo: def __init__(self): self.a = 1 self.__b = 1 def display(self): return self.__b obj = Demo()print(obj.__b) The program has an error because there isn’t any function to return self.aThe program has an error because b is private and display(self) is returning a private memberThe program has an error because b is private and hence can’t be printedThe program runs fine and 1 is printed
Question
class Demo: def init(self): self.a = 1 self.__b = 1 def display(self): return self.__b obj = Demo()print(obj.__b) The program has an error because there isn’t any function to return self.aThe program has an error because b is private and display(self) is returning a private memberThe program has an error because b is private and hence can’t be printedThe program runs fine and 1 is printed
Solution
The program has an error because b is private and hence can’t be printed. In Python, variables with double underscore before them are considered as private variables. They can't be accessed directly outside of the class, they can be accessed through a class method or a getter method. In this case, trying to print obj.__b directly will result in an AttributeError.
Similar Questions
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.a=45Clear ResponseSave & Next
What will be the output of the following Python code?class A: def __init__(self): self.__x = 1class B(A): def display(self): print(self.__x)def main(): obj = B() obj.display()main()10Error, invalid syntax for object declarationError, private class member can’t be accessed in a subclass
What will be the output of the following Python code?class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags)obj=fruits()obj.display() The program has an error because display() is trying to print a private class memberThe program runs fine but nothing is printedThe program runs fine and 5 is printed The program has an error because display() can’t be accessed
What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.
The program has an error because there isn’t any function to return self.aThe program has an error because b is private and display(self) is returning a private memberThe program has an error because b is private and hence can’t be printedThe program runs fine and 1 is printed
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.