博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Boost.Python
阅读量:6156 次
发布时间:2019-06-21

本文共 2646 字,大约阅读时间需要 8 分钟。

Introduction

  

The  Library binds C++ and Python in a mostly-seamless fashion. It is just one member of the boost C++ library collection at .  

Use the  Library to quickly and easily export C++ to Python such that the Python interface is very similar to the C++ interface. It is designed to be minimally intrusive on your C++ design. In most cases, you should not have to alter your C++ classes in any way in order to use them with Boost.Python. The system should simply reflect your C++ classes and functions into Python. Boost.Python bindings are written in pure C++, using no tools other than your editor and your C++ compiler.  

The Python  serves as a mailing list for users of the library. Documentation for the current release is available at . Development documentation, which is usually more up-to-date, is available through the  interface.   

 

Relationship to the Python C API

 

Python already provides an API for gluing together Python and C. So what is Boost::Python? Boost::Python is a wrapper for the Python/C API.  

Using the Python/C API, you have to deal with passing pointers back and forth between Python and C, and worry about pointers hanging out in one place when the object they point to has been thrown away. Boost::Python takes care of much of this for you. In addition, Boost::Python lets you write operations on Python objects in C++ in OOP style.

For example, using the Python/C API, to do the C++ equivalent of "y = object_x[i]", you might do:

 

y = PySequence_GetItem(object_x, i);

 

By contrast, in Boost::Python, you can just do:

 

y = object_x[i];

 

In addition, Boost::Python makes it easy to EXPORT your C++ classes into Python, without even changing them. Boost::Python is designed with the idea in mind that users never touch a PyObject*.

 

If you need to get to the underlying Python/C API

 

If you do use Boost::Python, though, you can still use stuff from the Python/C API in your C++ code. You don't even need to import the Python.h header file. Just use the functions. For example, to clear an error in Python that you caught in C++, you could do this in the middle of an otherwise purely Boost::Python program:

 

PyErr_Clear();

 

In C++, the Python/C API represents Python objects by  pointers. In Boost::Python, these are wrapped by instances of the boost::python::object class.

If you need the underlying  of any boost::python::object, you can get it via the ptr() method of boost::python::object, which returns a *. You can then use it in Python/C API calls. For example, to test if a boost::python::object called boostObj has an attribute called "myAttributeName", you can do:

 

PyObject_HasAttrString(boostObj.ptr(), "myAttributeName");

转载地址:http://zgifa.baihongyu.com/

你可能感兴趣的文章
SQL server 安装教程
查看>>
Linux下ftp和ssh详解
查看>>
跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击
查看>>
js时间和时间戳之间如何转换(汇总)
查看>>
js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用
查看>>
java中string和int的相互转换
查看>>
P1666 前缀单词
查看>>
HTML.2文本
查看>>
Ubuntu unity安装Indicator-Multiload
查看>>
解决Eclipse中新建jsp文件ISO8859-1 编码问题
查看>>
7.对象创建型模式-总结
查看>>
【论文阅读】Classification of breast cancer histology images using transfer learning
查看>>
移动端处理图片懒加载
查看>>
jQuery.on() 函数详解
查看>>
谈缓存和Redis
查看>>
【转】百度地图api,根据多点注标坐标范围计算地图缩放级别zoom自适应地图
查看>>
用户调研(补)
查看>>
ExtJS之开篇:我来了
查看>>
☆1018
查看>>
oracle 去掉空格
查看>>