Knowee
Questions
Features
Study Tools

What will be the output of the following Python code?class Demo: def __new__(self): self.__init__(self) print("Demo's __new__() invoked") def __init__(self): print("Demo's __init__() invoked")class Derived_Demo(Demo): def __new__(self): print("Derived_Demo's __new__() invoked") def __init__(self): print("Derived_Demo's __init__() invoked")def main(): obj1 = Derived_Demo() obj2 = Demo()main()Derived_Demo’s __init__() invokedDerived_Demo's __new__() invokedDemo's __init__() invokedDemo's __new__() invokedDerived_Demo's __new__() invokedDemo's __init__() invokedDemo's __new__() invokedDerived_Demo's __new__() invokedDemo's __new__() invokedDerived_Demo’s __init__() invokedDemo's __init__() invoked

Question

What will be the output of the following Python code?class Demo: def new(self): self.init(self) print("Demo's new() invoked") def init(self): print("Demo's init() invoked")class Derived_Demo(Demo): def new(self): print("Derived_Demo's new() invoked") def init(self): print("Derived_Demo's init() invoked")def main(): obj1 = Derived_Demo() obj2 = Demo()main()Derived_Demo’s init() invokedDerived_Demo's new() invokedDemo's init() invokedDemo's new() invokedDerived_Demo's new() invokedDemo's init() invokedDemo's new() invokedDerived_Demo's new() invokedDemo's new() invokedDerived_Demo’s init() invokedDemo's init() invoked

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

Solution

The provided Python code will result in a TypeError. This is because the __new__() method is a static method, not an instance method. It doesn't take self as a parameter.

The __new__() method is responsible for creating a new instance of the class. It's the first step of creating an instance, before __init__().

The __init__() method is then used to initialize the new object created by __new__().

In your code, you're calling self.__init__(self) in the __new__() method, which is incorrect because self doesn't exist yet.

Here's the corrected version of your code:

class Demo:
    def __new__(cls):
        print("Demo's __new__() invoked")
        return super(Demo, cls).__new__(cls)

    def __init__(self):
        print("Demo's __init__() invoked")

class Derived_Demo(Demo):
    def __new__(cls):
        print("Derived_Demo's __new__() invoked")
        return super(Derived_Demo, cls).__new__(cls)

    def __init__(self):
        print("Derived_Demo's __init__() invoked")

def main():
    obj1 = Derived_Demo()
    obj2 = Demo()

main()

This will output:

Derived_Demo's __new__() invoked
Derived_Demo's __init__() invoked
Demo's __new__() invoked
Demo's __init__() invoked

This problem has been solved

Similar Questions

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)

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

What will be the output of the following Python code?class program:  def __init__(self, param):    self.o1 = param class code(program):  def __init__(self, param):    self.o2 = param ct = code(22)print ("%d %d" % (ct.o1, ct.o2))OptionsNone NoneNone 2222 NoneError is generated

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 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/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.