的第三个参数PyList_SetItem
是要添加到列表中,这通常是从C型转换,如在这个简单的例子Python的对象:
/* This adds one to each item in a list. For example:
alist = [1,2,3,4,5]
RefArgs.MyFunc(alist)
*/
static PyObject * MyFunc(PyObject *self, PyObject *args)
{
PyObject * ArgList;
int i;
PyArg_ParseTuple(args, "O!", &PyList_Type, &ArgList));
for (i = 0; i < PyList_Size(ArgList); i++)
{
PyObject * PyValue;
long iValue;
PyValue = PyList_GetItem(ArgList, i);
/* Add 1 to each item in the list (trivial, I know) */
iValue = PyLong_AsLong(PyValue) + 1;
/* SETTING THE ITEM */
iRetn = PyList_SetItem(ArgList, i, PyLong_FromLong(iValue));
if (iRetn == -1) Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
PyObject_SetItem
是相似的。区别在于PyList_SetItem
是盗取的参考,但PyObject_SetItem
只是借用它。 PyObject_SetItem
不能与不可变对象一起使用,如元组。
如果我想将C对象(Atom)添加到列表中,PyLong_FromLong的等效函数是什么? – user2117235 2013-03-12 14:35:01