PyObject_CallMethod self问题

类函数的第1个参数是self,c++传参没有传self,

导致python接收参数错误

void main() {

	Py_Initialize();
	PyObject* module = PyImport_ImportModule("bbb");
	assert(module != NULL); // Returned non-null object

	PyObject* MyPyClass = PyObject_GetAttrString(module, "BasicStats");
	assert(MyPyClass != NULL);// Returned non-null object

	PyObject* myClassInstance = PyInstanceMethod_New(MyPyClass);
	assert(myClassInstance != NULL);// Returned non-null object

	PyObject* result = PyObject_CallMethod(myClassInstance, "AddItem", "(i)", 1);
	assert(result != NULL); // Failed - returned NULL

	result = PyObject_CallMethod(myClassInstance, "AddItem", "(i)", 2);
	assert(result != NULL); // Failed - returned NULL

	result = PyObject_CallMethod(myClassInstance, "get_Max", NULL);
	assert(result != NULL); // Failed - returned NULL

	printf("Min = %ld\n", PyLong_AsLong(result));
	printf("Min = %f\n", PyFloat_AsDouble(result));
	system("Pause");
}
class BasicStats:
    def __init__(self):
        self._m_min = self._m_max = self._m_sum = self._m_sumSqr = 0
        self._m_count = 0
        self.Reset()

    def Reset(self):
        self._m_min = float("inf")
        self._m_max = float("-inf")
        self._m_sum = self._m_sumSqr = 0
        self._m_count = 0

    def AddItem(value):
        print('add',value)
        return value
        # self._m_count += 1
        # if self._m_max < value:
        #     self._m_max = value
        # return self._m_count

    def get_Max(self):
        return self._m_max

 

你可能感兴趣的:(python,c++基础)