Note that there are some explanatory texts on larger screens.

plurals
  1. POPython (and Python C API): __new__ versus __init__
    primarykey
    data
    text
    <p>The question I'm about to ask seems to be a duplicate of <a href="https://stackoverflow.com/questions/674304/pythons-use-of-new-and-init">Python&#39;s use of __new__ and __init__?</a>, but regardless, it's still unclear to me exactly what the practical difference between <code>__new__</code> and <code>__init__</code> is.</p> <p>Before you rush to tell me that <code>__new__</code> is for creating objects and <code>__init__</code> is for initializing objects, let me be clear: <strong>I get that.</strong> In fact, that distinction is quite natural to me, since I have experience in C++ where we have <a href="https://stackoverflow.com/questions/222557/cs-placement-new">placement new</a>, which similarly separates object allocation from initialization. </p> <p>The <a href="http://docs.python.org/release/3.0.1/extending/newtypes.html" rel="noreferrer">Python C API tutorial</a> explains it like this:</p> <blockquote> <p>The new member is responsible for creating (as opposed to initializing) objects of the type. It is exposed in Python as the <code>__new__()</code> method. ... <strong>One reason to implement a new method is to assure the initial values of instance variables</strong>.</p> </blockquote> <p>So, yeah - I <em>get</em> what <code>__new__</code> does, but despite this, I <em>still</em> don't understand why it's useful in Python. The example given says that <code>__new__</code> might be useful if you want to "assure the initial values of instance variables". Well, isn't that exactly what <code>__init__</code> will do? </p> <p>In the C API tutorial, an example is shown where a new Type (called a "Noddy") is created, and the Type's <code>__new__</code> function is defined. The Noddy type contains a string member called <code>first</code>, and this string member is initialized to an empty string like so:</p> <pre><code>static PyObject * Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { ..... self-&gt;first = PyString_FromString(""); if (self-&gt;first == NULL) { Py_DECREF(self); return NULL; } ..... } </code></pre> <p>Note that without the <code>__new__</code> method defined here, we'd have to use <code>PyType_GenericNew</code>, which simply initializes all of the instance variable members to NULL. So the only benefit of the <code>__new__</code> method is that the instance variable will start out as an empty string, as opposed to NULL. <strong>But why is this ever useful, since if we cared about making sure our instance variables are initialized to some default value, we could have just done that in the <code>__init__</code> method?</strong> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload