PythonのC APIを使用してオブジェクトを作成します
-
08-10-2019 - |
質問
私は自分のオブジェクトレイアウトを次のように定義しているとします
typedef struct {
PyObject_HEAD
// Other stuff...
} pyfoo;
...そして私のタイプの定義:
static PyTypeObject pyfoo_T = {
PyObject_HEAD_INIT(NULL)
// ...
pyfoo_new,
};
の新しいインスタンスを作成するにはどうすればよいですか pyfoo
私のC拡張機能のどこか?
解決
電話 pyobject_new(), 、 に続く pyobject_init().
編集: 最良の方法はすることです 電話 Python自体のように、クラスオブジェクト:
/* Pass two arguments, a string and an int. */
PyObject *argList = Py_BuildValue("si", "hello", 42);
/* Call the class object. */
PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList);
/* Release the argument list. */
Py_DECREF(argList);
所属していません StackOverflow