你将学到什么

  • 在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. php简单实现通讯录采集,我的第一个php,适合新手

    起源于要整理通讯录,原先用的是文件调查,现在学了php,就自己试一下.程序短小精悍,适于学习.有两个文件,bj.html用于显示和采集表单信息.bj.php用于处理数据和反馈结果.突出之处在于可以上传 ...

  2. Cocos2dx版本与CocosBuilder版本匹配问题

    我用的是CocosBuilder 2.1版本,将ccbi导入到xcode 5中时提示: WARNING! Incompatible ccbi file version 警告;ccbi版本不兼容. 查看 ...

  3. webserver的编写中出现的问题

    在webserver编写过程中,出现过问题.就是标签<input>编写过程中少了name属性,导致程序无法读到<form>提交的数据.

  4. spring boot: 在maven中装入springframework框架

    1.在maven 的pom.xml中加入 <dependency> <groupId>org.springframework</groupId> <artif ...

  5. phalcon:官方多模块支models层,mode数据库配置(二)

    phalcon:官方多模块支models层,mode数据库配置(二) 利用:\pahlcon\mvc\model\Manager::registerNamespaceAlias()方法获取多模块下的m ...

  6. 深度学习—BN的理解(二)

    神经网络各个操作层的顺序: 1.sigmoid,tanh函数:conv -> bn -> sigmoid -> pooling 2.RELU激活函数:conv -> bn -& ...

  7. Python基础-os、sys模块

    一,os模块import os ,sysos.system('ipconfig')#执行操作系统命令,获取不到返回结果 os.popen()#也可以执行操作系统命令,可以返回命令执行结果,但需要rea ...

  8. Vue从接口请求数据

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. JSTL标签提示:"items" does not support runtime expressions

    今天在使用JSTL的 c:forEach 标签时,jsp提示:"items" does not support runtime expressions,后来才发现是因为taglib ...

  10. 【leetcode刷题笔记】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...