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程序——参数传递的更多相关文章
-
C++中嵌入python程序——命令行模式
http://blog.csdn.net/yiyouxian/article/details/51992721
-
在应用中嵌入Python:转
在应用中嵌入Python 前面的章节讨论如何扩展Python,如何生成适合的C库等.不过还有另一种情况:通过将Python嵌入C/C++应用以扩展程序的功能.Python嵌入实现了一些使用Python ...
-
如何在 Go 中嵌入 Python
如果你看一下 新的 Datadog Agent,你可能会注意到大部分代码库是用 Go 编写的,尽管我们用来收集指标的检查仍然是用 Python 编写的.这大概是因为 Datadog Agent 是一个 ...
-
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下的[计划任务]功能 之后点击右侧的[创建基本任务 ...
随机推荐
-
MySQL入门04-MySQL主从配置
1.主节点授权同步用户 2.主节点修改配置文件 3.从节点修改配置文件 4.主节点锁定后查看状态 5.从节点配置并启动同步.查看状态 6.主节点解锁并验证从节点是否正常同步 环境:CentOS 6.7 ...
-
Photoshop 使用可选颜色
CMYK 由 青色(Cyan) 洋红(Magenta)黄色(Yellow)黑色(Key Plate)组成, 他们和三基色 红绿蓝 RGB 关系如下 如图 红色 = 品红 + 黄色 绿色 = 黄色 + ...
-
JQuery插件开发 - 模板
(function($) { $.fn.PluginName = function(options) { // 创建一个默认设置对象 var defaults = { key : "Defa ...
-
Tomcat下使用Druid配置JNDI数据源
com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...
-
python之迭代器、生成器、面向过程编程
一 迭代器 一 迭代的概念 #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而不 ...
-
Hadoop基础-MapReduce入门篇之编写简单的Wordcount测试代码
Hadoop基础-MapReduce入门篇之编写简单的Wordcount测试代码 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本文主要是记录一写我在学习MapReduce时的一些 ...
-
Understanding Flash: Blocks, Pages and Program / Erases
https://flashdba.com/2014/06/20/understanding-flash-blocks-pages-and-program-erases/ In the last pos ...
-
NSProxy应用例子
动态代理模式的应用很多,特别是在不能修改被代理类的前提下,要对执行某些方法时需要打log或者捕捉异常等处理时,是一个非常方便的方法.只需要少量修改客户端(场景类)代码和添加一个代理类就可以实现,这个符 ...
-
Ajax-ajax实例2-根据邮政编码获取地区信息
项目结构 运行效果: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - ajaxexample_2 ******* ...
-
20155212 实验一《Java开发环境的熟悉》实验报告
20155212 实验一<Java开发环境的熟悉>实验报告 命令行下Java程序开发 命令创建实验目录 输入mkdir 2051212创建以自己学号命名的文件夹,通过cd 20155212 ...