Knowee
Questions
Features
Study Tools

class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

Question

class Demo: def init(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

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

Solution 1

The code you've provided is written in Python. It defines a class Demo with an initializer method, a private attribute __b, a public attribute a, and a method get to return the value of the private attribute.

Here's a step-by-step explanation:

  1. class Demo: This line is defining a new class named Demo.

  2. def __init__(self): This line is defining the initializer method for the class. This method is called when an object of the class is created.

  3. self.a = 1 This line is creating a public attribute a for the class and initializing it with the value 1.

  4. self.__b = 1 This line is creating a private attribute __b for the class and initializing it with the value 1. In Python, private attributes are denoted by double underscores before the attribute name.

  5. def get(self): This line is defining a method get for the class.

  6. return self.__b This line is returning the value of the private attribute __b when the get method is called.

  7. obj = Demo() This line is creating an object obj of the class Demo.

  8. obj.a=45 This line is changing the value of the public attribute a of the object obj to 45.

  9. print(obj.a) This line is printing the value of the attribute a of the object obj, which should output 45.

Please note that the code you've provided is not properly formatted, which might cause syntax errors when running it. Here's the properly formatted version:

class Demo:     
    def __init__(self):         
        self.a = 1         
        self.__b = 1     

    def get(self):         
        return self.__b

obj = Demo()
obj.a=45
print(obj.a)

This problem has been solved

Solution 2

The code you've provided is written in Python and it defines a class Demo with an initializer method, a method get, and then creates an instance of Demo and modifies one of its attributes. Here's a step-by-step explanation:

  1. class Demo: This line is defining a new class named Demo.

  2. def __init__(self): This line defines the initializer method for the class. This method is called when an instance of the class is created.

  3. self.a = 1 and self.__b = 1 These lines are setting the initial values of the instance variables a and __b. The double underscore before b makes it a private variable, meaning it can only be accessed within the class.

  4. def get(self): This line defines a new method for the class named get.

  5. return self.__b This line returns the value of the private variable __b.

  6. obj = Demo() This line creates a new instance of the Demo class and assigns it to the variable obj.

  7. obj.a=45 This line changes the value of the a attribute of the obj instance to 45.

  8. print(obj.a) This line prints the value of the a attribute of the obj instance, which should be 45.

Please note that your code is missing a line break before obj = Demo(). It should be on a new line.

This problem has been solved

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 Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

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.a=45

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

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.