16

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__?

martineau
  • 119,623
  • 25
  • 170
  • 301
oridamari
  • 561
  • 7
  • 12
  • 24
  • 2
    Check this link: http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init – Vinkal Mar 01 '15 at 08:45
  • `__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 01 '15 at 09:24

3 Answers3

24

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.

Eithos
  • 2,421
  • 13
  • 13
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.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Which class do you refer to when you say that `__new__` is called passing the **class** as the first parameter? – HelloGoodbye Aug 22 '17 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 '17 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__

glglgl
  • 89,107
  • 13
  • 149
  • 217
Oleksii M
  • 1,458
  • 14
  • 22