top of page
Writer's pictureNazeeruddin

__new__ vs __init__ when to use in Python?

Updated: Feb 10, 2023

The __new__ () creates a new instance.

The __init__ () method initializes 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 class level method, which means that it is called on the class, rather than on an instance of the class.


The default implementation of __new__ is sufficient, no need to override it.


for example, to create a singleton class, we can use it to create only one instance.

The above code overrides the __new__ method to make sure there is only one instance. So, s1 == s2 statement is True.



 
Thanks for reading!!!

Your Rating and Review will be appreciated!!

Twitter: @LearnerLandmark
241 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Commenting has been turned off.
bottom of page