你将学到什么

  • 在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. 兼容IE8及其他浏览器的回车事件

    //阻止默认浏览器行为 function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) { // ...

  2. 一个用 vue 写的树层级组件 vue-ztree

    最近看了大神的关于vue-ztree的博客,感觉很赞,于是摘抄下来,方便自己学习,机智girl,哈哈哈O(∩_∩)O 最近由于后台管理项目的需要,页面需要制作一个无限树的需求,我第一感就想到了插件 z ...

  3. php常用函数htmlspecialchars、strip_tags、addslashes解析

    本文章向大家介绍php开发中经常使用到的字符串函数htmlspecialchars.strip_tags.addslashes的使用方法及他们之间的区别,需要的朋友可以参考一下. 1.函数strip_ ...

  4. codeforces 622B B. The Time

    B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. type_traits.h

    type_traits.h // Filename: type_traits.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ht ...

  6. DP小合集

    1.Uva1625颜色的长度 dp[i][j]表示前一个串选到第i个 后一个串选到第j个 的最小价值 记一下还有多少个没有结束即dp2 记一下每个数开始和结束的位置 #include<cstdi ...

  7. UML图之例图

    用例图主要说明的是谁要使用系统,以及他们使用该系统可以做些什么,帮助开发团队以一种可视化的方式理解系统的功能需求. 一个用例图包含了多个模型元素,如系统.参与者和用例,并且显示这些元素之间的各种关系, ...

  8. ACM学习历程—广东工业大学2016校赛决赛-网络赛D 二叉树的中序遍历(数据结构)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=3 这算是一个胡搞类型的题目.当然肯定是有其数据结构支撑的. 唯一的限制就是 ...

  9. 【LeetCode】020. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. 【LeetCode】019. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...