조금 혼란 스럽습니다. 일부 C++ 및 Python 통합을 시도하고 있지만 간단하지 않습니다. Boost :: Python을 제대로 컴파일 할 수 없으므로 Boost를 사용하지 않습니다. 그러나 그것은 또 다른 이야기입니다.PyObject의 C++ 및 Python 검사 유형이 실패합니다.
현재, 여기에 내가 C++로하고있어 무엇 :
//set everything up
PyObject* py_main_module = PyImport_AddModule("__main__");
PyObject* py_global_dict = PyModule_GetDict(py_main_module);
PyObject* py_local_dict = PyDict_New();
PyObject* py_return_value;
PyRun_SimpleString(data.c_str()); //runs Python code, which defines functions
//call a function defined by the python code
py_return_value = PyRun_String("test()", Py_single_input, py_global_dict, py_local_dict);
//attempt to check the type of the returned value
if(py_return_value != NULL) {
//this is the problem: all of these print 0
cout << PyList_Check(py_return_value) << endl;
cout << PySet_Check(py_return_value) << endl;
cout << PyFloat_Check(py_return_value) << endl;
} else {
cout << "IT WAS NULL?!" << endl;
}
파이썬 프로그램 (라는 문자열로 C++ 프로그램 "데이터"에 입력) :
def test():
derp = 1.234
#derp = [1, 2, 3, 4]
#derp = set([1, 2, 3, 4])
return derp
자,가 문제는 형식 검사가 작동하지 않는다는 것입니다. 파이썬 함수가 부동 소수점,리스트 또는 집합을 반환하는지 여부에 관계없이 모두 0을 반환합니다. 내가 도대체 뭘 잘못하고있는 겁니까?
누구나 PyRun_String을 호출하면 콘솔에 반환 된 값이 인쇄되는 이유를 알 수 있습니다. 정말 짜증나.
고마워요! 그것은 정말로 내 문제 모두를 해결합니다. 반환 값 외에도 명령문이나 표현식으로 평가하는 것의 차이점은 무엇입니까? 아니면 그 유일한 차이점은 무엇입니까? –
표현식은 값을 생성 할 수 있습니다. 예제는'x - 1','foo (blah)','lambda x : y'입니다. 문장은 Python의 일반적인 코드 행이며,'x = 1'이라는 할당과 함수 정의를 포함합니다. 표현 자체는 명령문이 될 수 있지만 모든 명령문이 표현식으로 취급 될 수있는 것은 아닙니다. (표현식은 함수에 전달할 수있는 것으로 생각할 수 있습니다.) – nneonneo