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?
The Python/C API allows for compiled pieces of code to be called from Python programs or executed within the CPython interpreter. This process of producing compiled code for use by CPython is generally known as "extending" Python and the compiled pieces of code to be used are known as "extension modules".
CAPI (Common Application Programming Interface) is an international standard interface that application s can use to communicate directly with ISDN equipment. Using CAPI, an application program can be written to initiate and terminate phone calls in computers equipped for ISDN.
PyObject is an object structure that you use to define object types for Python. All Python objects share a small number of fields that are defined using the PyObject structure. All other object types are extensions of this type. PyObject tells the Python interpreter to treat a pointer to an object as an object.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With