I have a doubt regarding the tp_init pointer in the struct PyTypeObject.
Does it point to the user defined _init _ or some other ??
jeff5
(Jeff Allen)
April 4, 2026, 4:16pm
3
Yes, it points to the C implementation of __init__. Of course, it bears a slightly complex relationship to tp_new and type.__call__, but that is just the data model.
static int
object_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyTypeObject *type = Py_TYPE(self);
if (excess_args(args, kwds)) {
if (type->tp_init != object_init) {
PyErr_SetString(PyExc_TypeError,
"object.__init__() takes exactly one argument (the instance to initialize)");
return -1;
}
if (type->tp_new == object_new) {
PyErr_Format(PyExc_TypeError,
"%.200s.__init__() takes exactly one argument (the instance to initialize)",
type->tp_name);
return -1;
}
}
return 0;
}
Now if the type is defined in Python, with an __init__ in Python, then this slot is made to contain a pointer to a wrapper function (in C) that dispatches to that Python method.
static int
slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *res = call_method(self, &_Py_ID(__init__), args, kwds);
if (res == NULL)
return -1;
if (res != Py_None) {
PyErr_Format(PyExc_TypeError,
"__init__() should return None, not '%.200s'",
Py_TYPE(res)->tp_name);
Py_DECREF(res);
return -1;
}
Py_DECREF(res);
return 0;
}
thankyou….was looking for this…
thankyou!! This made the picture clearer!!