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 Implementation

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)
🧐 Not the exact question you are looking for?Go ask a question

Solution

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

Similar Questions

class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags)obj=fruits()obj.display()

what is the output of following python code? class myclass:    def __init__(self,a):        self.a = a        print(self.a)o=myclass()

Which of the following is correct?class A:    def __init__(self,name):        self.name=namea1=A("john")a2=A("john")

class Student:    def __init__(self,name,id):        self.name=name        self.id=id        print(self.id)std=Student("Simon",1)std.id=2print(std.id)

class Dog:def __init__(self):# missing line hered = Dog()print(d.age)Fill in the missing line of code, so that the output for this code is 5.

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.