Info
Open the page on your phone

What is the difference between __init__() and __new__()?

In Python, __new__() is responsible for creating a new instance of a class, while __init__() is responsible for initializing the newly created object. __new__() is a static method that takes the class itself as its first parameter and is used to create an instance, while __init__() is an instance method that is called after the instance has been created to initialize its attributes.

                        
class MyClass:
    def __new__(cls):
        # creating a new instance
        return super(MyClass, cls).__new__(cls)

    def __init__(self):
        # initializing the newly created object
        self.value = 10