C ++ 코드를 Python 라이브러리로 변환 할 때 SWIG를 사용하는 C ++ / Python 라이브러리 프로젝트를 개발 중입니다. C ++ 헤더 중 하나에는 아래와 같은 전역 상수 값이 있습니다.
const int V0 = 0;
const int V1 = 1;
const int V2 = 2;
const int V3 = 3;
const int V[4] = {V0, V1, V2, V3};
Python에서 직접 V0에서 V3까지 사용할 수 있지만 V
.
>>> import mylibrary
>>> mylibrary.V0
0
>>> mylibrary.V[0]
<Swig Object of type 'int *' at 0x109c8ab70>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'SwigPyObject' object has no attribute '__getitem__'
누구든지 V
Python 튜플 또는 목록 으로 자동으로 변환하는 방법을 알려줄 수 있습니까? 내 .i
파일 에서 무엇을해야 합니까?
다음 매크로가 작동했습니다.
%{
#include "myheader.h"
%}
%define ARRAY_TO_LIST(type, name)
%typemap(varout) type name[ANY] {
$result = PyList_New($1_dim0);
for(int i = 0; i < $1_dim0; i++) {
PyList_SetItem($result, i, PyInt_FromLong($1[i]));
} // i
}
%enddef
ARRAY_TO_LIST(int, V)
%include "myheader.h"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다