[英]Create an object using Python's C API
Say I have my object layout defined as:假设我的对象布局定义为:
typedef struct {
PyObject_HEAD
// Other stuff...
} pyfoo;
...and my type definition: ...和我的类型定义:
static PyTypeObject pyfoo_T = {
PyObject_HEAD_INIT(NULL)
// ...
pyfoo_new,
};
How do I create a new instance of pyfoo
somewhere within my C extension?如何在我的 C 扩展中的某处创建一个新的
pyfoo
实例?
Call
PyObject_New() , followed by
PyObject_Init() .
调用
PyObject_New() ,然后调用
PyObject_Init
() 。
EDIT: The best way is to call the class object, just like in Python itself:编辑:最好的方法是调用类对象,就像在 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.