Knowee
Questions
Features
Study Tools

What do these lines print?class Base(): """ My base class """ __nb_instances = 0 def __init__(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesfor i in range(3): b = Base()print(b.id)

Question

What do these lines print?class Base(): """ My base class """ __nb_instances = 0 def init(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesfor i in range(3): b = Base()print(b.id)

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

Solution

The code defines a class Base with a private class variable __nb_instances which is incremented each time an instance of the class is created. The instance variable id is set to the current number of instances.

The for loop creates three instances of the Base class.

The print(b.id) statement prints the id of the last created instance.

So, the output of this code will be 3.

Similar Questions

What do these lines print?class Base(): """ My base class """ __nb_instances = 0 def __init__(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesclass User(Base): """ My User class """ def __init__(self): super().__init__() self.id = 89u = User()print(u.id)

class Base(): """ My base class """ __nb_instances = 0 def __init__(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesclass User(Base): """ My User class """ def __init__(self): super().__init__() self.id = 89u = User()print(u.id)

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

What will be the output of the following code?class MyClass:    class_variable = 10    def __init__(self, instance_variable):        self.instance_variable = instance_variableobj1 = MyClass(20)obj2 = MyClass(30)MyClass.class_variable = 40print(obj1.class_variable, obj2.class_variable)

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

1/2

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.