구조체의 벡터를 반환하는 메서드가있는 C++ 클래스가 있습니다. 내 현재의 cython 구현에서 구조체는 괜찮 았지만 괜찮은 사전으로 끝나고, 나는 구조체를 파이썬 객체로 받고 싶습니다.객체를 cython으로 랩핑하다
현재 설정은 다음과 같습니다.
나에게 사전의 목록을 제공합니다 파이썬에서 myclass.myclass_function()
를 호출이 경우
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()
을 cpp_mycode.pxd, 각각은 키 '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
type`을 입력하고 의미있는 결과를 얻으십시오.
는 지금 내가 볼 수있는 유일한 해결책은 내 수업의 목록, 각각 __dict__.update
치료를 받고를 생산하기 위해 어떻게 든 self.thisptr.myclass_function()
에 의해 반환 된 목록이 answer의 __dict__.update
일부를 사용하여 포장에서 온다. 그러나 이것을하기위한 더 우아하고 내장 된 cython 방식이 있어야한다고 생각합니다. 어떤 아이디어?
솔루션은 원래 사전을 객체에 저장합니다.이 사전은 기본적으로 사전처럼 작동합니다. __getitem__은'[]'유형 호출을 구현하기 때문입니다. 이것은 타입을 검사 할 수있는 문제를 해결할 것이지만 실제로 struct와 같은 C로 만들지는 않습니다. – fbence