What is the difference between __init__() and __new__() in Python?
Richard W
richard w profile pic

In Python,__init__() and__new__() are special methods used in the creation of objects, but they serve different purposes and have different functionalities. Here's a detailed explanation of the differences between__init__() and__new__(): __new__() method: The__new__() method is a static method that is responsible for creating and initializing a new instance of a class before__init__() is called. It is called first during the object creation process and returns a newly created object. The__new__() method is typically used in immutable classes or when customizing the object creation process.

1
2
3
4
5
6
7
8
9
10

class MyClass:
    def __new__(cls, *args, **kwargs):
   instance = super().__new__(cls)
   # Custom initialization logic here
   return instance

    def __init__(self, *args, **kwargs):
   # Initialization logic here
   pass

In this example,__new__() is defined as a static method that creates a new instance of theMyClass class. The method calls the__new__() method of the parent class usingsuper().__new__(cls) to ensure the correct creation of the object. Custom initialization logic can be added inside__new__() if needed. __init__() method: The__init__() method is an instance method that is called after the object is created by__new__(). It initializes the newly created object by setting its initial state or performing any necessary setup operations. The__init__() method is commonly used to accept arguments and initialize instance variables.

1
2
3
4
5
6
7
8
9

class MyClass:
    def __new__(cls, *args, **kwargs):
   instance = super().__new__(cls)
   return instance

    def __init__(self, arg1, arg2):
   self.arg1 = arg1
   self.arg2 = arg2

In this example,__init__() is an instance method that initializes thearg1 andarg2 instance variables of theMyClass object. The method is called after the object is created by__new__(). Order of execution: The order of execution between__new__() and__init__() is as follows: 1. The__new__() method is called first and creates a new instance of the class. 2. The newly created instance is then passed as the first argument to the__init__() method. 3. The__init__() method initializes the object's state using the passed arguments and performs any necessary setup operations. Use cases: The__new__() method is typically used in scenarios where you need more control over the creation of objects, such as customizing the object creation process or working with immutable objects. On the other hand, the__init__() method is commonly used to initialize instance variables, set the initial state of the object, or perform any required setup operations. It's important to note that in most cases, you won't need to override the__new__() method. The default implementation provided by the parent class is sufficient for creating objects. The__init__() method is often the primary method used for object initialization. In summary,__new__() is responsible for object creation and customization, while__init__() is responsible for initializing the object's state. They serve different purposes and are called at different stages of the object creation process in Python.