When it comes to constructors of Python classes, all of us know the __init__ method, but fewer developers know the __new__ method.
The difference between these two methods is simple:
The __new__() method creates a new instance.
The __init__() method initialises that instance.
The __new__ method is a special method that is called before the __init__ method. It is responsible for creating the object and returning it. The __new__ method is a static method, which means that it is called on the class, rather than on an instance of the class.
In general, we don’t need to override the __new__ method. Because in most cases, the default implementation of it is sufficient.
If you need some more precise control of your classes, overriding the __new__ method is also a good choice.
For example, if you would like to apply the singleton pattern to a Python class, you may implement it as follows: