你将学到什么

  • 在C++中调用Python代码时的返回值问题

基础类型

修改Python脚本(build/zoo.py)

def rint():
return 2 def rstr():
return "fwd" if __name__ == '__main__':
pass

修改源文件(main.cpp)

#include <iostream>
#include <boost/python.hpp> 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 x = module.attr("rint")();
int rx = extract<int>(x);
std::cout << "rint: " << rx << std::endl;
object y = module.attr("rstr")();
std::string rs = extract<std::string>(y);
std::cout << "rstr: " << rs << std::endl;
}
catch (const error_already_set&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}

标准库

修改Python脚本(build/zoo.py)

def rvec():
return [1, 2, 3, "fwd"] if __name__ == '__main__':
pass

修改源文件(main.cpp)

#include <iostream>
#include <vector>
#include <string>
#include <boost/python.hpp> 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 v = module.attr("rvec")();
boost::python::handle<> v_iter(PyObject_GetIter(v.ptr()));
std::cout << "rvec: ";
while (true)
{
handle<> py_hdl(allow_null(PyIter_Next(v_iter.get())));
if (PyErr_Occurred())
throw_error_already_set();
if (!py_hdl.get())
break;
object py_obj(py_hdl);
object r = py_obj.attr("__str__")();
std::string rs = extract<std::string>(r);
std::cout << rs << " ";
}
std::cout << std::endl;
}
catch (const error_already_set&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}

Boost Python学习笔记(五)的更多相关文章

  1. Boost Python学习笔记(四)

    你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...

  2. Boost Python学习笔记(二)

    你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...

  3. Boost Python学习笔记(三)

    你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...

  4. python学习笔记五 模块上(基础篇)

    模块学习 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  5. Python学习笔记五

    一. 递归 递归函数: def a (): print ("from b") b() def b(): print("from a ") a() a() 递推和 ...

  6. Python学习笔记五:错误与异常

    一:常见异常与错误 BaseException 所有异常的基类SystemExit 解释器请求退出KeyboardInterrupt 用户中断执行(通常是输入^C)Exception 常规错误的基类S ...

  7. python学习笔记(五):装饰器、生成器、内置函数、json

    一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...

  8. Boost Python学习笔记(一)

    开发环境搭建 下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install ...

  9. Python学习笔记五(读取提取写入文件)

    #Python打开读取一个文件内容,然后写入一个新的文件中,并对某些字段进行提取,写入新的字段的脚本,与大家共同学习. import os import re def get_filelist(dir ...

随机推荐

  1. A N EAR -D UPLICATE D ETECTION A LGORITHM T O F ACILITATE D OCUMENT C LUSTERING——有时间看看里面的相关研究

    摘自:http://aircconline.com/ijdkp/V4N6/4614ijdkp04.pdf In the syntactical approach we define binary at ...

  2. 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 ...

  3. 我对java的理解(一)——注解就是贴标签

    在现实生活中,贴标签这种现象比比皆是.去超市,去商场,每个或者每类物品都会有它的标签,甚至在我们自己身上也会有标签,比如,程序猿.逗逼.单身狗.80/90后.屌丝……呵呵,太多了.有时候,我们也会戏谑 ...

  4. vc6++Release和Debug

    1. 如何快速地规范代码缩进格式 选中所需要规范的代码,按shift+F8 2. 如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框 ...

  5. 实现两个窗口通信方法-postMessage

    此方案可解决跨域而且跨Iframe,而且http和https之间的交互 首先来看一下基本的语法 otherWindow.postMessage(message, targetOrigin, [tran ...

  6. 2016北京集训 小Q与进位制

    题目大意 一个数每一位进制不同,已知每一位的进制,求该数的十进制表达. 显然有 $$Ans=\sum\limits_{i=0}^{n-1}a_i \prod\limits_{j=0}^{i-1}bas ...

  7. Agc_006 E Rotate 3x3

    题目大意 给定一个$3\times N$的方阵,每个位置的数恰好是每一个$[1,3\times N]$中的数. 初始时,每个位置$[x,y]$填的是$3(y-1)+x,(1\leq x\leq N,1 ...

  8. CodeForces - 1017 C. The Phone Number(数学)

    Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...

  9. XP系统下显示文件或文件的安全选项卡

    在很多的时候,我们需要设置文件或文件夹的权限,这里一般就要用到安全选项卡,但在xp系统下,默认是不显示的,如何调出我们的“安全”选项卡呢? 具体做法:点击“工具”菜单下的"文件夹选项(o). ...

  10. 洛谷【P1142】轰炸

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...