Erstellen Sie ein Objekt unter Verwendung von Python-C-API
-
08-10-2019 - |
Frage
sagen, ich habe meine Aufgabe Layout definiert als:
typedef struct {
PyObject_HEAD
// Other stuff...
} pyfoo;
... und meine Typdefinition:
static PyTypeObject pyfoo_T = {
PyObject_HEAD_INIT(NULL)
// ...
pyfoo_new,
};
Wie erstelle ich eine neue Instanz von pyfoo
irgendwo in meiner C-Erweiterung?
Lösung
Anruf PyObject_New () , gefolgt von PyObject_Init () .
EDIT: Der beste Weg ist, um Aufruf das Klassenobjekt, wie in Python selbst:
/* 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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow