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?

War es hilfreich?

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
scroll top