Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializer vs Constructor [duplicate]

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

like image 919
oridamari Avatar asked Mar 01 '15 08:03

oridamari


People also ask

Is a constructor and initializer the same?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.

Does initializer list call copy constructor?

And if we use Initializer List there are only two function calls: copy constructor + destructor call.

Are initializer lists faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment. Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float .

What are the advantages of initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.


2 Answers

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.

like image 89
Eithos Avatar answered Sep 28 '22 11:09

Eithos


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

like image 22
6502 Avatar answered Sep 28 '22 11:09

6502