As title. More specifically, could dict_init in dictobject.c return safely kwds if this is not NULL, and if args is NULL or empty? If so, maybe this will speedup keyword-only dict construction.
dict.__init__() does not return a dict object. It updates already created dict object. What exactly change do you propose?
You’re right, I confused with __new__. I was thinking about something like this:
static PyObject *
dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *self;
PyDictObject *d;
PyDictObject *kwdict = (PyDictObject *)kwds;
const int kwds_size = ((kwds != NULL)
? kwdict->ma_used
: 0
);
PyObject* arg = NULL;
if (! PyArg_UnpackTuple(args, __func__, 0, 1, &arg)) {
return NULL;
}
const int arg_size = ((arg != NULL)
? PyObject_Length(arg)
: 0
);
if (kwds_size && ! arg_size) {
return kwdict;
}
[...]
}
static int
dict_init(PyObject *self, PyObject *args, PyObject *kwds)
{
if (self == kwds) {
return 0;
}
return dict_update_common(self, args, kwds, "dict");
}
It will cause the behavior change.
kwargs = {'a': 1}
d = dict(**kwargs)
assert d['a'] == 1
kwargs['a'] = 2
assert d['a'] == 2
I see. I thought that the dictionary passed with the “star-star” operator was copied. Thank you for the explaining.