C++传递参数给Python

1.总结

C++传递参数给Python,需要转换成PyObject *类型。比如,C++的 int 是一个整数,该值占用4个字节的存储空间,而一个 python 的 int 实际是一个 PyObject* 指向 12字节。
前 4个字节是整数,代表引用次数;中间4个字节是指向 int 类型定义的指针,最后 4个字节是才是这个 int 的值。
所以 C++ 和 Python 之间参数互相传递都需要 Python提供的 api。

2.案例

PyObject* arg1 = Py_BuildValue("i",220);  //整数参数
PyObject* arg2 = Py_BuildValue("f", 3.14);  //浮点数参数
PyObject* arg3 = Py_BuildValue("s", "hello"); //字符串参数

3. 其他案例

4.其他数据类型

s (str or None) [const char *]
Convert a null-terminated C string to a Python str object using 'utf-8' encoding. If the C string pointer is NULL, None is used.

s# (str or None) [const char *, int or Py_ssize_t]
Convert a C string and its length to a Python str object using 'utf-8' encoding. If the C string pointer is NULL, the length is ignored and None is returned.

y (bytes) [const char *]
This converts a C string to a Python bytes object. If the C string pointer is NULL, None is returned.

y# (bytes) [const char *, int or Py_ssize_t]
This converts a C string and its lengths to a Python object. If the C string pointer is NULL, None is returned.

z (str or None) [const char *]
Same as s.

z# (str or None) [const char *, int or Py_ssize_t]
Same as s#.

u (str) [const wchar_t *]
Convert a null-terminated wchar_t buffer of Unicode (UTF-16 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is NULL, None is returned.

u# (str) [const wchar_t *, int or Py_ssize_t]
Convert a Unicode (UTF-16 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.

U (str or None) [const char *]
Same as s.

U# (str or None) [const char *, int or Py_ssize_t]
Same as s#.

i (int) [int]
Convert a plain C int to a Python integer object.

b (int) [char]
Convert a plain C char to a Python integer object.

h (int) [short int]
Convert a plain C short int to a Python integer object.

l (int) [long int]
Convert a C long int to a Python integer object.

B (int) [unsigned char]
Convert a C unsigned char to a Python integer object.

H (int) [unsigned short int]
Convert a C unsigned short int to a Python integer object.

I (int) [unsigned int]
Convert a C unsigned int to a Python integer object.

k (int) [unsigned long]
Convert a C unsigned long to a Python integer object.

L (int) [long long]
Convert a C long long to a Python integer object.

K (int) [unsigned long long]
Convert a C unsigned long long to a Python integer object.

n (int) [Py_ssize_t]
Convert a C Py_ssize_t to a Python integer.

c (bytes of length 1) [char]
Convert a C int representing a byte to a Python bytes object of length 1.

C (str of length 1) [int]
Convert a C int representing a character to Python str object of length 1.

d (float) [double]
Convert a C double to a Python floating point number.

f (float) [float]
Convert a C float to a Python floating point number.

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 with void*) 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} (dict) [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.

参考:

https://www.pythonf.cn/read/156686

https://docs.python.org/3/c-api/arg.html?highlight=py_buildvalue#c.Py_BuildValue

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值