Create an object using Python's C API

后端 未结 1 883
一整个雨季
一整个雨季 2020-11-29 03:35

Say I have my object layout defined as:

typedef struct {
    PyObject_HEAD
    // Other stuff...
} pyfoo;

...and my type definition:

<
相关标签:
1条回答
  • 2020-11-29 03:54

    Call PyObject_New(), followed by PyObject_Init().

    EDIT: The best way is to call the class object, just like in Python itself:

    /* 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);
    
    0 讨论(0)
提交回复
热议问题