我有一個C++類,它有一個返回結構向量的方法。在我當前的cython實現中,結構最終成爲字典,這是可以的,但不是最好的,我想以python對象的形式接收結構。cython將struct結構作爲對象
我目前的設置看起來像這樣。
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()
在這種情況下Python調用myclass.myclass_function()
會給我的詞典列表,每一個都具有關鍵'mystruct_property', which is functional, but a) it would be much better to be able to call it as a property
.mystruct_property and also would be nice to be able to call a
類型`並獲得有意義的結果。
現在我能看到的唯一的解決方案來自於使用本answer的__dict__.update
部分和包裝由self.thisptr.myclass_function()
莫名其妙地返回的列表產生我的類的列表,每個得到__dict__.update
治療。但有人會認爲,這樣做必須有更優雅和內置的循環方式。有任何想法嗎?
您的解決方案將原始字典存儲在一個對象中,該對象基本上像字典一樣工作,因爲__getitem__實現'[]'類型的調用。這將解決能夠檢查類型的問題,但並不真正使它成爲類似C的結構。 – fbence