c调用python参数传递_【转】C++中嵌入python程序——参数传递

本文介绍了在C++中嵌入Python程序时如何进行参数传递,包括使用`PyObject_CallMethod`和`PyObject_CallFunction`函数,以及如何处理多个参数、元组和字典类型。同时讲解了不同类型的参数类型说明符,如`s`、`i`、`O`等,帮助创建灵活的C++/Python混合程序。

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

C++中嵌入python程序——命令行模式

http://blog.csdn.net/yiyouxian/article/details/51992721

在应用中嵌入Python:转

在应用中嵌入Python 前面的章节讨论如何扩展Python,如何生成适合的C库等.不过还有另一种情况:通过将Python嵌入C/C++应用以扩展程序的功能.Python嵌入实现了一些使用Python ...

c++中嵌入python

c++ 中嵌入python  :  https://blog.csdn.net/yiyouxian/article/category/6324494 Python C 和线程 :http://www. ...

Windows系统中设置Python程序定时运行方法

Windows系统中设置Python程序定时运行方法 一.环境 win7 + Python3.6 二.步骤 1,在Windows开始菜单中搜索“计划任务”,并且点击打开“计划任务”: 2.点击“创建基 ...

在 C 代码中嵌入 Python 语句或使用 Python 模块 (Visual Studio 2013 环境设置)

1) 新建一个 内嵌 Python 语句的 C 代码, // This is a test for check insert the Python statements or module in C. ...

【python之路2】CMD中执行python程序中文显示乱码

在IDLE中执行下面代码,中文显示正常: # -*- coding:utf-8 -*- st=raw_input("请输入内容")print st 但在CMD中执行e:\hello ...

一种在BIOS中嵌入应用程序的方法及实现

本文针对Award公司开发的计算机系统BIOS提出了一种嵌入应用程序的方法,其基本原理对别的品牌的BIOS也一样适用,仅需稍加修改.文中作者给出并讨论一个完整的例子程序,该程序已经通过实验验证.  正 ...

如何在Windows系统中设置Python程序定时运行

文章出处:http://blog.csdn.net/wwy11/article/details/51100432 首先,我们肯定是要用到Windows下的[计划任务]功能 之后点击右侧的[创建基本任务 ...

在VS2017中编写Python程序

最近开始了python的学习,在搭建完python环境之后,在选择IDE的时候陷入了困境,首先选择的是PyCharm但是用着还是不习惯,毕竟用VS开发了几年了,突然换软件总感觉有点不适应,就想到了强大 ...

随机推荐

Android Material design

1.Material Design:扁而不平 2.Android Support Design 库 之 Snackbar使用及源码分析 3.十大Material Design开源项目,直接拿来用!

导出证书Cer文件为Pem格式的步骤

(1)先导出Push Services的证书,比如我们命名为“magic_cert.p12”,注意导出时会让你输入密码. (2)再导出Push Services证书的密钥(Private Key),比 ...

Android Non-UI to UI Thread Communications(Part 1 of 5)

original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-1-of-5/ ANDRO ...

Ubuntu14.04 安装 PHP cURL

今天遇到 Fatal error: Call to undefined function curl_init() in /xxx/xxxx/www/application/library/Ku/Htt ...

浅述Oracle分布式事务概念

着系统的复杂性不断增加,我们所面对的分布式系统渐渐增加.分布式文件系统.分布式消息队列系统等等层出不穷,在一些行业特别是互联网行业应用广泛.分布式数据库也是目前使用比较常用的分布式系统之一. 简单来说 ...

RAID常用级别的比较

[转]RAID常用级别的比较 特点 硬盘及容量 性能及安全 典型应用 raid 0 用于平行存储,即条带.其原理是把连续的数据分成几份,然后分散存储到阵列中的各个硬盘上.任何一个磁盘故障,都将导致数据 ...

常用到的html页面布局和组件: 自己用

1. 用div当作圆

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值