The Python/C API has a number of related functions that perform similar operations where one is usually for general use and another is somehow more efficient or convenient for a specific situation.
For example, PyDict_SetItem
and PyDict_SetItemString
are the same but the latter is specialized for a C string as a key. For PyList_SetItem
and PyList_SET_ITEM
, the second has no error checking and doesn't decref the existing item (explained here).
But what is the reason for having special conversions like PyInt_FromLong
when there is always Py_BuildValue
? Both return a new reference to a Python object and seem to do the exact same thing. This same question applies to functions like PyInt_FromSsize_t
, PyTuple_Pack
, etc. (all compared to Py_BuildValue
with the appropriate format string of course).
When should you use one over the other?