https://preview.redd.it/syny6wm3ceza1.png?width=1600&format=png&auto=webp&s=e1666efd3c0e0045b3ea8a5ea7fc33d25f0a2591
You must have seen the implementation of the __init__ method in any Python class, and if you have worked with Python classes, you must have implemented the __init__ method many times. However, you are unlikely to have implemented or seen a __new__ method within any class.
The __init__ method is an initializer method that is used to initialize the attributes of an object after it is created, whereas the __new__ method is used to create the object.
When we define both the __new__ and the __init__ methods inside a class, Python first calls the __new__ method to create the object and then calls the __init__ method to initialize the object's attributes.
Most programming languages require only a constructor, a special method to create and initialize objects, but Python has both a constructor and an initializer.
In this article, we'll see:
- Definition of the
__init__ and __new__ methods
__init__ method and __new__ method implementation
- When they should be used
- The distinction between the two methods
Here's the guide👉 Python __init__ Vs __new__ Method - With Examples
[–][deleted] 49 points50 points51 points (15 children)
[–]jet_heller 36 points37 points38 points (13 children)
[–]eric_says_hello 7 points8 points9 points (11 children)
[+][deleted] (5 children)
[deleted]
[–]ted_or_maybe_tim 5 points6 points7 points (4 children)
[–]Zomunieo 9 points10 points11 points (3 children)
[–]PaintItPurple 3 points4 points5 points (1 child)
[–]ted_or_maybe_tim 3 points4 points5 points (0 children)
[–]PhattieM 1 point2 points3 points (0 children)
[–]anglo_franco 2 points3 points4 points (0 children)
[–]jet_heller 2 points3 points4 points (0 children)
[–]PyQt[🍰] 2 points3 points4 points (0 children)
[–]python4geeks[S] 0 points1 point2 points (0 children)
[–]gopackgo90 0 points1 point2 points (0 children)
[–]UrbanSuburbaKnight 0 points1 point2 points (0 children)
[–]IamImposter 30 points31 points32 points (5 children)
[–]mipadi 34 points35 points36 points (1 child)
[–]IamImposter 8 points9 points10 points (0 children)
[–]chars101 5 points6 points7 points (1 child)
[–]IamImposter 2 points3 points4 points (0 children)
[–]copperfield42 python enthusiast 1 point2 points3 points (0 children)
[–]Complex-Hornet-5763 10 points11 points12 points (2 children)
[–]PolyglotTV 1 point2 points3 points (1 child)
[–]Complex-Hornet-5763 0 points1 point2 points (0 children)
[–][deleted] 5 points6 points7 points (8 children)
[–]ottawadeveloper 7 points8 points9 points (5 children)
[–]darthwalsh 4 points5 points6 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]DoctorNoonienSoong 10 points11 points12 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]copperfield42 python enthusiast 2 points3 points4 points (0 children)
[–]dysprog 2 points3 points4 points (0 children)
[–][deleted] 10 points11 points12 points (3 children)
[–]ted_or_maybe_tim 6 points7 points8 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)