[cpp]
例5:typedic.c // create the dictionary PyObject* pDict = PyDict_New(); // new reference assert(PyDict_Check(pDict)); // add a few named values PyDict_SetItemString(pDict, "first", Py_BuildValue("i", 2003)); PyDict_SetItemString(pDict, "second", Py_BuildValue("f", 3.14f)); // enumerate all named values PyObject* pKeys = PyDict_Keys(); // new reference for(int i = 0; i < PyList_Size(pKeys); ++i) { PyObject *pKey = PyList_GetItem(pKeys, i); PyObject *pValue = PyDict_GetItem(pDict, pKey); assert(pValue); } Py_DECREF(pKeys); // remove a named value PyDict_DelItemString(pDict, "second"); // cleanup Py_DECREF(pDict);用C语言扩展Python的功能(3)
内容版权声明:除非注明,否则皆为本站原创文章。