1

I wonder how I can create a PyObject in C++ and then return it to Python.

Sadly the documentation is not very explicit about it. There is no PyObject_Create so I wonder whether allocating sizeof(PyObject) via PyObject_Malloc and initializing the struct is sufficient.

For now I only need an object with functions attached.

abergmeier
  • 13,224
  • 13
  • 64
  • 120

1 Answers1

1

Do you really want a (1) PyObject, as in what Python calls object, or (2) an object of some subtype? That you "need an object with functions attached" seems to indicate you want either methods or attributes. That needs (2) in any case. I'm no expert on the C API, but generally you'd define your own PyTypeObject, then create an instance of that via PyObject_New (refcount and type field are initialized, other fields you might add are not).