C++中嵌入python程序——參數傳遞
前面兩篇博客已經介紹如何在C++中嵌套使用 python,但是在實際使用中,我們需要向python傳遞各種各樣的參數,這樣的程序才具有更高的靈活性。下面簡單介紹一下參數傳遞,整體代碼不再給出,只介紹幾個核心語法,只要掌握就能與前面代碼結合起來生成完整可用的代碼。
PyObject_CallMethod(pClass, “class_method”, “O”, pInstance)
參數分別為 PyObject(類),string(類方法),string(O表示參數為PyObject) ,PyObject(類實例)
PyObject_CallFunction(pFun, “O”, pyores)
參數分別為 PyObject(函數),string(O表示參數為PyObject) ,PyObject(函數中使用的參數)
問題來了,函數有多個參數怎么辦,參數怎么傳遞?別擔心,有多少個我們就寫多少個:
PyObject_CallFunction(pFun, “OO…O”, PyObject 1,PyObject 2…PyObject n)
中間的O可替換成參數類型說明符中的任意一個,比如字符串s,int型變量i等等
創建一個元組
PyObject *pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, Py_BuildValue(“i”, 1));//0—序號 i表示創建int型變量
PyTuple_SetItem(pArgs, 1, Py_BuildValue(“i”, 2));
PyTuple_SetItem(pArgs, 2, Py_BuildValue(“i”, 3));
PyEval_CallObject(pFunc, pArgs); //調用函數,pArgs元素個數與被調函數參數個數一致
PyObject *pDict = PyDict_New(); //創建字典類型變量
PyDict_SetItemString(pDict, “Name”, Py_BuildValue(“s”, “Zhangsan”)); //往字典類型變量中填充數據
PyDict_SetItemString(pDict, “Address”, Py_BuildValue(“s”, “BeiJing”));
將上述字典賦值給元組
PyObject *pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, pDict)
python返回給C++的是PyObject類型,如果我想在純C++程序里使用它怎么辦:(可以使用下面這個函數
char * PyString_AsString(PyObject*)
至此,利用上面的幾個基本參數傳遞方法,已經可以創建靈活性較強的C++/python程序。
傳入參數類型說明符
百度一下發現相關的內容一大堆,從這里http://www.cnblogs.com/lancelod/p/4036922.html摘抄了下來
s (string) [char *]
Convert a null-terminated C string to a Python object. If the C string pointer is NULL, None is used.
s# (string) [char *, int]
Convert a C string and its length to a Python object. If the C string pointer is NULL, the length is ignored and None is returned.
z (string or None) [char *]
Same as s.
z# (string or None) [char *, int]
Same as s#.
u (Unicode string) [Py_UNICODE *]
Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is NULL, Noneis returned.
u# (Unicode string) [Py_UNICODE *, int]
Convert a Unicode (UCS-2 or UCS-4) data buffer and its length to a Python Unicode object. If the Unicode buffer pointer is NULL, the length is ignored and None is returned.
i (integer) [int]
Convert a plain C int to a Python integer object.
b (integer) [char]
Convert a plain C char to a Python integer object.
h (integer) [short int]
Convert a plain C short int to a Python integer object.
l (integer) [long int]
Convert a C long int to a Python integer object.
B (integer) [unsigned char]
Convert a C unsigned char to a Python integer object.
H (integer) [unsigned short int]
Convert a C unsigned short int to a Python integer object.
I (integer/long) [unsigned int]
Convert a C unsigned int to a Python integer object or a Python long integer object, if it is larger than sys.maxint.
k (integer/long) [unsigned long]
Convert a C unsigned long to a Python integer object or a Python long integer object, if it is larger than sys.maxint.
L (long) [PY_LONG_LONG]
Convert a C long long to a Python long integer object. Only available on platforms that support long long.
K (long) [unsigned PY_LONG_LONG]
Convert a C unsigned long long to a Python long integer object. Only available on platforms that support unsigned long long.
n (int) [Py_ssize_t]
Convert a C Py_ssize_t to a Python integer or long integer.
New in version 2.5.
c (string of length 1) [char]
Convert a C int representing a character to a Python string of length 1.
d (float) [double]
Convert a C double to a Python floating point number.
f (float) [float]
Same as d.
D (complex) [Py_complex *]
Convert a C Py_complex structure to a Python complex number.
O (object) [PyObject *]
Pass a Python object untouched (except for its reference count, which is incremented by one). If the object passed in is a NULL pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception. Therefore, Py_BuildValue()will return NULL but won’t raise an exception. If no exception has been raised yet, SystemError is set.
S (object) [PyObject *]
Same as O.
N (object) [PyObject *]
Same as O, except it doesn’t increment the reference count on the object. Useful when the object is created by a call to an object constructor in the argument list.
O& (object) [converter, anything]
Convert anything to a Python object through a converter function. The function is called with anything (which should be compatible withvoid *) as its argument and should return a “new” Python object, or NULL if an error occurred.
(items) (tuple) [matching-items]
Convert a sequence of C values to a Python tuple with the same number of items.
[items] (list) [matching-items]
Convert a sequence of C values to a Python list with the same number of items.
{items} (dictionary) [matching-items]
Convert a sequence of C values to a Python dictionary. Each pair of consecutive C values adds one item to the dictionary, serving as key and value, respectively.
轉自:http://blog.csdn.net/yiyouxian/article/details/51995029