I have a c++ class, that has a method which returns a vector of structs. In my current cython implementation the structs end up as dictionaries, which is ok, but not the best, I'd like to receive the structs as python objects.
My current setup looks like this.
cpp_mycode.pxd
from libcpp.vector cimport vector
cdef extern from "myheader.h":
cdef struct mystruct:
int mystruct_property
cdef cppclass myclass:
myclass()
vector[mystruct] myclass_function()
mycode.pyx
cimport cpp_mycode
cdef class myclass:
cdef cpp_mycode.myclass *thisptr
def __cinit__(self):
self.thisptr = new cpp_myclass.myclass()
def __dealloc(self):
if self.thisptr is not NULL:
delf self.thisptr
def myclass_function(self):
return self.thisptr.myclass_function()
In this case calling myclass.myclass_function()
from python will give me a list of dictionaries, each having the key 'mystruct_property', which is functional, but a) it would be much better to be able to call it as a property
.mystruct_propertyand also would be nice to be able to call a
type` and get a meaningful result.
Right now the only solution I can see comes from using the __dict__.update
part of this answer and wrapping the list returned byself.thisptr.myclass_function()
somehow to produce a list of my class, each getting the __dict__.update
treatment. But one would think that there must be a more elegant and built-into-cython way of doing this. Any ideas?