Why are constructors really called "constructors"?
The constructor (named __new__ ) creates and returns a new instance of the class. So the class method C.__new__ is a constructor for class C.
C.__init__ instance C.__init__ is called for a specific instance after it is created to initialize it before passing it to the caller. So this method is the initializer for new instances of C.
How do they differ from the methods in the class?
As stated in the official documentation, __init__ is called after the instance is created. Other methods do not receive this treatment.
What is their purpose?
The purpose of the C.__new__ is to define user behavior when creating a new C instance
The purpose of the C.__init__ is to define the user initialization of each C instance after it is created.
For example, Python allows you to do:
class Test(object): pass t = Test() tx = 10
But if you want each Test instance to have an x attribute of 10, you can put this code in __init__ :
class Test(object): def __init__(self): self.x = 10 t = Test() print tx
Each instance method (the method invoked for a particular instance of the class) receives the instance as the first argument. This argument is conditionally called self
Class methods, such as the __new__ constructor, instead receive the class as the first argument.
Now, if you need custom values ββfor the x attribute, all you have to do is pass that value as the __init__ argument:
class Test(object): def __init__(self, x): self.x = x t = Test(10) print tx z = Test(20) print tx
I hope this helps you resolve some doubts, and since you have already received good answers to other questions, I will stop here :)