你将学到什么

  • 在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. QQ.PC管家进程

    1.家里的笔记本 WIn7x64 C:\Program Files (x86)\Tencent\QQPCMgr\12.10.19266.225\QMDL.exeC:\Program Files (x8 ...

  2. 恢复delete删除的数据

    SELECT * FROM tablename AS OF TIMESTAMP TO_TIMESTAMP('2010-12-15 11:10:17', 'YYYY-MM-DD HH:MI:SS')

  3. php: +1天, +3个月, strtotime(): +1 day, +3 month

    php: +1天, +3个月, strtotime():  +1 day, +3 month 比如,我现在当前时间基础上+1天: strtotime("+1 day"); 比如我现 ...

  4. vuex使用mapActions报错解决办法

    先贴上报错: vuex2增加了mapGetters和mapActions的方法,借助stage2的Object Rest Operator 所在通过 methods:{ ...mapActions([ ...

  5. 【leetcode刷题笔记】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  6. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. Statement

    题目大意 给定一棵基环外向树,和若干组询问,对于每次独立的询问都指定一些起点和一些终点,你删去一些边,使得从任意起点出发都无法到达终点,并让删去的边的编号的最小值最大,求这个最大的最小值. 题解 不难 ...

  8. luogu1754卡特兰数

    卡特兰数 打表 滑稽 #include<iostream> #include<cstdio> #include<algorithm> #include<cst ...

  9. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

  10. Oracle中spool命令实现的两种方法比较

    ---恢复内容开始--- 要输出符合要求格式的数据文件只需在select时用字符连接来规范格式.比如有如下表 SQL>; select id,username,password from myu ...