Knowee
Questions
Features
Study Tools

What will be the output of the following Python code?class A: def __repr__(self): return "1"class B(A): def __repr__(self): return "2"class C(B): def __repr__(self): return "3"o1 = A()o2 = B()o3 = C()print(obj1, obj2, obj3)1 1 11 2 3‘1’ ‘1’ ‘1’An exception is thrown

Question

What will be the output of the following Python code?class A: def repr(self): return "1"class B(A): def repr(self): return "2"class C(B): def repr(self): return "3"o1 = A()o2 = B()o3 = C()print(obj1, obj2, obj3)1 1 11 2 3‘1’ ‘1’ ‘1’An exception is thrown

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

Solution

The code you provided has a small mistake. The variables o1, o2, and o3 are defined, but obj1, obj2, and obj3 are being printed. If we correct this mistake, the output of the code will be "1 2 3".

Here's the corrected code:

class A:    
    def __repr__(self):        
        return "1"

class B(A):    
    def __repr__(self):        
        return "2"

class C(B):    
    def __repr__(self):        
        return "3"

o1 = A()
o2 = B()
o3 = C()

print(o1, o2, o3)

This will output: 1 2 3

This is because the __repr__ method is used to tell Python how to represent an object of a class as a string. Each class (A, B, C) has its own __repr__ method that returns a different string ("1", "2", "3"). When we print an object of each class, the corresponding __repr__ method is called.

This problem has been solved

Similar Questions

What will be the output of the following Python code?class A: def one(self): return self.two()  def two(self): return 'A' class B(A): def two(self): return 'B'obj1=A()obj2=B()print(obj1.two(),obj2.two())A AA BB BAn exception is thrown

What will be the output of the following Python code?class A: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return 1 def __eq__(self, other): return self.x * self.y == other.x * other.yobj1 = A(5, 2)obj2 = A(2, 5)print(obj1 == obj2)False1TrueAn exception is thrown

class A: def one(self): return self.two() def two(self): return 'A' class B(A): def two(self): return 'B'obj2=B()print(obj2.two())AAn exception is thrownA BB

what will be the output of the following code?class Myclass:    x=0    def incre(self):        self.x+=1    def disp(self):        print(self.x)ob1=Myclass()ob1.incre()ob1.disp()ob2=Myclass()ob2.incre()ob2.disp()

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)

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.