Knowee
Questions
Features
Study Tools

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b)

Question

What will be the output of the following Python code?class Demo: def init(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b)

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The code will raise an AttributeError. This is because __b is a private variable, and it cannot be accessed directly outside of its class, Demo.

In Python, variables that start with double underscores __ are considered as private variables. They are used to encapsulate (hide) the data from the outside world (outside of the class they are defined).

So, when you try to access obj.__b, Python will raise an AttributeError saying 'Demo' object has no attribute '__b'.

If you want to access the value of __b, you should use the display method defined in the class Demo, like this: print(obj.display()). This will print 1, which is the value of __b.

This problem has been solved

Solution 2

The code will raise an AttributeError. This is because '__b' is a private variable. In Python, variables that start with double underscores are considered private for the class they are declared in and cannot be accessed directly outside the class.

Here's the step by step explanation:

  1. A class named 'Demo' is defined with an initializer method (init), which sets 'a' to 1 and '__b' also to 1. The variable 'a' is public and '__b' is private due to the double underscores.

  2. A method named 'display' is defined which returns the value of the private variable '__b'.

  3. An object 'obj' of the class 'Demo' is created.

  4. When you try to print 'obj.__b', it will raise an AttributeError saying 'Demo' object has no attribute '__b'. This is because '__b' is private and cannot be accessed directly outside the class.

If you want to access the value of '__b', you should use the 'display' method of the class 'Demo' like this: print(obj.display()). This will print '1' as the output.

This problem has been solved

Similar Questions

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()

What will be the output of the following Python code?class A: def __init__(self): self.__i = 1 self.j = 5  def display(self): print(self.__i, self.j)class B(A): def __init__(self): super().__init__() self.__i = 2 self.j = 7 c = B()c.display()2 71 51 72 5

What is the output of the following code?        python    class A:        def display(self):            print("Class A")    class B(A):        def display(self):            print("Class B")    class C(A):        def display(self):            print("Class C")    obj = B()    obj.display()    obj = C()    obj.display()

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__bobj = Demo()print(obj.a)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 runs fine and 1 is printedThe program has an error as you can’t name a class member using __b

What will be the output of the following code?class MyClass:    def __init__(self, value=5):        self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)

1/3

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.