What can `__init__` do that `__new__` cannot?
Per When to use __new__ vs. __init__
__new__
is the first step of instance creation. It's called first, and is responsible for returning a new instance of your class. In contrast,__init__
doesn't return anything; it's only responsible for initializing the instance after it's been created.
Also the class of class is type
, and type.__call__()
is implemented something like below refer to the above description:
def __call__(cls, *args, **kwargs):
obj = cls.__new__(cls, *args, **kwargs)
if isinstance(obj, cls):
obj.__init__(*args, **kwargs)
return obj
We know __init__()
just do a part of __new__()
can do anything to that object before the object is returned.
In general, you shouldn't need to override
__new__
unless you're subclassing an immutable type like str, int, unicode or tuple.
So it is not good to remove __init__
from the language, and it is better to always use __init__()
better than using __new__()
.
Here is one history of Low-level constructors and __new__()
.
Note about difference between __new__ and __init__
Before explaining missing functionality let's get back to definition of __new__
and __init__
:
__new__ is the first step of instance creation. It's called first, and is responsible for returning a new instance of your class.
However, __init__ doesn't return anything; it's only responsible for initializing the instance after it's been created.
Consequences of replacing __init__ with __new__
Mainly you would lose out on flexibility. You would get a lot of semantics headaches and loose separation of initializatin and construction (by joining __new__ and
init we are to joining construction and initialization into one step...).
Let's take a look on snippet below:
class A(object):
some_property = 'some_value'
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls, *args, **kwargs)
obj.some_property = cls.some_property
return obj
class B(A):
some_property = 2
def __new__(cls, *args, **kwargs):
obj = super(B, cls).__new__(cls)
return obj
Consequences of moving __init__
actions into __new__
:
Initialize
B
beforeA
: When you are using__new__
method instead of__init__
your first step of creating new instance of B is callingA.__new__
as side effect you cannot initializeB
beforeA
is initialized ( access and assign some properties to new B instance). Using of__init__
gives you such flexability.Loose control on initializing order: let's imagine that you have
B_N
inherited from two classes (A_N1
,A_N2
), now you would miss controlling of order of initializing new instance ofB_N
(what is the order you are going to initialize instances ? it could be matter... what is weird.)Properties and methods mess: you would miss access to
A.some_property
(cls
would be equal toB
while instantiating new instance ofB
. However directly accessing ofA.some_property
is possible, but my guess it's at least weird to access properties within class throught class name and not by usingclassmethods
).You cannot re-initialize an existed instance without creating new one or implementation special logic for this ( thanks to @platinhom for idea )
What can __init__
do that __new__
cannot?
There are no actions that cannot be done in __new__
and can in __init__
, because actions that __init__
performs is a subset of the actions that can be performed by __new__
.
An interesting moment from Python Docs, Pickling and unpickling normal class instances#object.getinitargs regarding when __init__
could be usefull:
When a pickled class instance is unpickled, its init() method is normally not invoked.
Everything you can do in __init__
can also be done in __new__
.
Then, why use __init__
?
Because you don't have to store instance in variable (obj
in your example code), and later bother returning it. You can focus on what you realy want to do – initializing mutable object.