17

I have heard that the __init__ function in python is not a Constructor, It's an Initializer and actually the __new__ function is the Constructor and the difference is that the __init__ function is called after the creation of the object and the __new__ called before. Am I right? Can you explain the difference better and why do we need both __new__ and __init__?

2
  • 2
  • __init__ is a direct equivalent to what other OO languages (especially Java and C++) tend to call a 'constructor'. AFAIK those languages don't really have any direct equivalent to __new__. But probably exactly because the 'constructor' equivalent doesn't really construct anything (and neither do C++ or Java 'constructors'), Python tends to refer to these both as the actual method names rather than calling either "the constructor".
    – lvc
    Mar 1, 2015 at 9:24

3 Answers 3

25

In essence, __new__ is responsible for creating the instance (thus, it may be accurate to say that it is the constructor, as you've noted) while __init__ is indeed a way of initializing state in an instance. For example, consider this:

class A(object):

    def __new__(cls):
        return object.__new__(cls)

    def __init__(self):
        self.instance_method()

    def instance_method(self):
        print 'success!'

newA = A()

Notice that __init__ receives the argument self, while __new__ receives the class (cls). Since self is a reference to the instance, this should tell you quite evidently that the instance is already created by the time __init__ gets called, since it gets passed the instance. It's also possible to call instance methods precisely because the instance has already been created.

As to your second question, there is rarely a need in my experience to use __new__. To be sure, there are situations where more advanced techniques might make use of __new__, but those are rare. One notorious example where people might be tempted to use __new__ is in the creation of the Singleton class (whether that's a good technique or not, however, isn't the point).

For better or worse, you basically get to control the process of instantiation, and whatever that might mean in your specific situation.

3

__init__ is called with an already built up instance of the object as first parameter (normally called self, but that's just a parameter name).

__new__ instead is called passing the class as first parameter and is expected to return an instance (that will be later passed to __init__).

This allows for example __new__ to return an already-existent instance for value-based objects that are immutable and for which identity shouldn't play a role.

2
  • Which class do you refer to when you say that __new__ is called passing the class as the first parameter? Aug 22, 2017 at 16:48
  • @HelloGoodbye: the class of the object being built. This can be the same class where you place __new__ or a class derived from it (in case neither the derived nor other bases listed before this class defined a __new__)
    – 6502
    Aug 22, 2017 at 20:38
1

Considering documentation on class customization to more details.

According to that these methods are used in these way: __new__() to create object, and __init__() to customize it.

And as 6502 noticed, __new__ is the special case of static method and gets class as its argument, while __init__ gets object produced by __new__

Not the answer you're looking for? Browse other questions tagged or ask your own question.