Boost Python学习笔记(三)
你将学到什么
- 在C++中调用Python代码时的传参问题
基础类型
继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add
和Str
函数,分别针对整数、浮点数和字符串参数的测试
def Add(x, y):
print(x + y)
def Str(s):
print("Output: " + s)
if __name__ == '__main__':
pass
然后修改下main.cpp
源文件
#include <iostream>
#include <boost/python.hpp>
#include "boost_wrapper.h"
using namespace boost::python;
using namespace boost::python::detail;
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
{
std::cout << "Initialize failed" << std::endl;
return -1;
}
try
{
object sys_module = import("sys");
str module_directory(".");
sys_module.attr("path").attr("insert")(1, module_directory);
object module = import("zoo");
module.attr("Add")(object(2), object(4));
module.attr("Add")(object(3.0f), object(4));
module.attr("Str")(object("test"));
}
catch (const error_already_set&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
编译并测试
# cd build
# make
# ./core
6
7.0
Output: test
类实例
首先修改下Python脚本(zoo.py),添加Pet
函数,针对类实例参数的测试,其参数为Animal类实例
import boost
def Pet(obj):
obj.eat("meat")
print(type(obj))
print(isinstance(obj, boost.Animal))
if __name__ == '__main__':
pass
然后修改下main.cpp
源文件
#include <iostream>
#include <boost/python.hpp>
#include "boost_wrapper.h"
using namespace boost::python;
using namespace boost::python::detail;
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
{
std::cout << "Initialize failed" << std::endl;
return -1;
}
try
{
object sys_module = import("sys");
str module_directory(".");
sys_module.attr("path").attr("insert")(1, module_directory);
object module = import("zoo");
object o = class_<AnimalWrap, boost::noncopyable>("Animal", init<std::string>())
.def("call", &Animal::call)
.def("move", &Animal::move)
.def("eat", &Animal::eat)("Wangcai");
module.attr("Pet")(o);
}
catch (const error_already_set&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
编译并测试
# cd build
# make
# ./core
Wangcai: eat meat
<class 'Animal'>
False
标准库
首先修改下Python脚本(zoo.py),添加tList
、tDict
和tTuple
函数,分别用于测试std::vector
/std::list
、std::map
以及数组
def tList(l):
for i in l:
print(i)
def tDict(d):
for k in d:
print(str(k) + ":" + str(d[k]))
def tTuple(t):
for i in t:
print(i)
if __name__ == '__main__':
pass
然后修改下main.cpp
源文件
#include <iostream>
#include <vector>
#include <boost/python.hpp>
#include "boost_wrapper.h"
using namespace boost::python;
using namespace boost::python::detail;
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
{
std::cout << "Initialize failed" << std::endl;
return -1;
}
try
{
object sys_module = import("sys");
str module_directory(".");
sys_module.attr("path").attr("insert")(1, module_directory);
object module = import("zoo");
list l;
l.append(2);
l.append("dog");
std::vector<int> v = {3, 4, 5, 6};
for (auto item : v)
l.append(item);
module.attr("tList")(l);
dict d;
d.setdefault("fwd", 28);
d.setdefault("xb", 26);
module.attr("tDict")(d);
tuple t = make_tuple("fwd", 28, "xb", 26);
module.attr("tTuple")(t);
}
catch (const error_already_set&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
编译并测试
# cd build
# make
# ./core
2
dog
3
4
5
6
fwd:28
xb:26
fwd
28
xb
26
总结
类实例还是尽量导出后在Python脚本中创建,如果在C++代码中创建,然后传入Python脚本的话,它的类型并不是boost.Animal
,这就导致无法使用isinstance
来区分对象。
Boost Python学习笔记(三)的更多相关文章
- Python 学习笔记三
笔记三:函数 笔记二已取消置顶链接地址:http://www.cnblogs.com/dzzy/p/5289186.html 函数的作用: 給代码段命名,就像变量給数字命名一样 可以接收参数,像arg ...
- Boost Python学习笔记(四)
你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...
- Boost Python学习笔记(五)
你将学到什么 在C++中调用Python代码时的返回值问题 基础类型 修改Python脚本(build/zoo.py) def rint(): return 2 def rstr(): return ...
- Boost Python学习笔记(二)
你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...
- webdriver(python) 学习笔记三
知识点:简单的对象定位 对象的定位应该是自动化测试的核心,要想操作一个对象,首先应该识别这个对象.一个对象就是一个人一样,他会有各种的特征(属性),如比我们可以通过一个人的身份证号,姓名,或者他住在哪 ...
- python学习笔记三--字典
一.字典: 1. 不是序列,是一种映射, 键 :值的映射关系. 2. 没有顺序和位置的概念,只是把值存到对应的键里面. 3. 通过健而不是通过偏移量来读取 4. 任意对象的无序集合 5. 可变长,异构 ...
- Python学习笔记三
一. 为什么要使用函数? 函数可以方便阅读代码. 函数可以减少重复代码. 函数可以减少管理操作,减少修改操作. 二. 函数分类: 内置函数:len() sum() max() min() ...
- python学习笔记(三)、字典
字典是一种映射类型的数据类型.辣么什么是映射呢?如果看过<数据结构与算法>这一本书的小伙伴应该有印象(我也只是大学学习过,嘻嘻). 映射:就是将两个集合一 一对应起来,通过集合a的值,集合 ...
- Python学习笔记三:模块
一:模块 一个模块就是一个py文件,里面定义了一些业务函数.引用模块,可以用import语句导入.导入模块后,通过 模块.函数名(参数) 来使用模块中的函数.如果存在多个同名模块,则前面模块名需要加 ...
随机推荐
- 关于jvm中的常量池和String.intern()理解
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- 红米1S刷机
1. http://www.miui.com/thread-7371342-1-1.html http://www.miui.com/download-226.html#306 http://www. ...
- Python- Anacoda环境使用Selenium+ChromeDriver报错
我的系统是win10,python是用Anacoda安装的,通过pip安装了selenium 后使用Chromedriver发现报错,pip安装selenium如下: pip install sele ...
- java学习进度条四
- Building Performant Expand & Collapse Animations
t's starting to be pretty common knowledge that there are only 2 things you can animate cheaply in C ...
- BEC listen and translation exercise 45
So the Counselling Services we offer deal with any problems arising from your studies or in your lif ...
- curl常用命令行总结
curl 有时HTTP服务接口写完,需要验证下接口功能,这个使用用curl最合适了 curl 全称 command line url viewer curl www.taobao.com curl w ...
- ffpanel --ffmpeg的GUI,让ffmpeg离开黑黑的命令行
程序及源码下载地址 :https://github.com/langsim/ffpanel from:http://blog.csdn.net/langsim/article/details/47 ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- [转]七个对我最好的职业建议(精简版)--Nicholas C. Zakas
一.不要别人点什么,就做什么 我的第一份工作,只干了8个月,那家公司就倒闭了.我问经理,接下来我该怎么办,他说: "小伙子,千万不要当一个被人点菜的厨师,别人点什么,你就烧什么.不要接受那样 ...