Several C types in the CPython source have a
__sizeof__
sys.getsizeof
METH_NOARG
void* whatever
itertools.product.__sizeof__
static PyObject *
product_sizeof(productobject *lz, void *unused)
{
Py_ssize_t res;
res = _PyObject_SIZE(Py_TYPE(lz));
res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
return PyLong_FromSsize_t(res);
}
static PyMethodDef product_methods[] = {
/* ... */
{"__sizeof__", (PyCFunction)product_sizeof, METH_NOARGS, sizeof_doc},
{NULL, NULL} /* sentinel */
};
sys.getsizeof
There's no specific type of function that only takes a single parameter, PyCFunction
s always take two arguments as the documentation states.
The METH_NOARGS
case doesn't mean that the function will only have a single parameter, rather, it means that the second parameter will always be NULL
:
The first parameter is typically named self and will hold a reference to the module or object instance. In all cases the second parameter will be NULL.
you can also see this directly in call.c
:
case METH_NOARGS:
// After snipping checks away
result = (*meth) (self, NULL);
There's a number of discussions covering this, see here, here and here for more.
As for the versions that only have a single argument, as Martijn points out, these use argument clinic to hide that.