pybind11使用教程笔记__4_数据类型转换(typeconversion)

这篇博客详细介绍了pybind11中不同类型的转换方法,包括C++原生类型到Python包装类型、Python原生类型到C++以及两者间的直接转换。pybind11推荐在各自语言中使用原生类型,但这种转换可能涉及数据拷贝,对于大型数据结构需要注意性能影响。
摘要由CSDN通过智能技术生成

Type conversions 类型转换

除了实现 python和C++的函数调用,两种语言之间的数据类型转换也很重要。通常有三种实现方法。

  • 在程序的所有地方均使用C++的数据类型,这样的话相应的类型必须进行打包,这样才可以在python中进行调用。
  • 在程序的所有地方均使用python的数据类型,这样的话相应的类型必须进行打包,这样才可以在C++中进行调用。
  • 在C++程序中使用C++的数据类型,在python中使用python的数据类型。(pybind11 推荐

type conversion 使用起来最自然,在各自的语言中使用原生的数据类型。但是这种方法的劣势是python和C++之间的相互调用数据必须要拷贝数据,因为相同的数据类型在python和C++中memory layout会有区别。

overview

  1. Native type in C++, wrapper in Python

    使用 py::class_来将C++数据类型打包给python使用,具体见 Object-oriented code section。 底层的数据结构是C++原生的数据结构, py::class_ wrapper 提供了一个python使用的接口。

    对于一个object-like的数据类型,python使用时pybind11会自动给C++原生的数据结构添加外围warper。当从python中将数据取回到C++中时,会自动去掉外层的warper。

  2. Wrapper in C++, native type in Python
    和上面的情况正好相反,这种情况主要利用python的原生数据类型,比如tuple和list。主要可以通过py::object warper 系列使C++能够使用python的原生数据类型。更多细节见: Python types section, 如下是一个简单的例子:

    void print_list(py::list my_list) {
        for (auto item : my_list)
            std::cout << item << " ";
    }
    
    >>> print_list([1, 2, 3])
    1 2 3
    

    上例中,python的数据类型list的数据格式没有被转变,只是在C++中使用 py::list class 将其包裹,其内核仍然是python的object。对py::list 对象进行copy 会像在python中一样做一个counting-reference的操作。将py::list 对象返回给python只需要将对象外边包裹的代码去除掉就可以了。

  3. Converting between native C++ and Python types
    在C++和python中各自使用各自的原生数据类型,交互时对各自原生的数据类型进行转换。

    void print_vector(const std::vector<int> &v) {
        for (auto item : v)
            std::cout << item << "\n";
    }
    
    >>> print_vector([1, 2, 3])
    1 2 3
    

    这种情况下,pybind11 会在C++中构建一个新的new std::vector<int> ,并从python 的list 中copy每个元素。新构建的std::vector会被传入到print_vector()中。在C++到python的数据流中,会发生类似的操作,python会创建一个list对象以匹配C++返回的数据。
    很多类似的数据类型转换都是支持的,而且都是开包即用,如下表所示。这些数据类型转换非常方便,但是都需要copy数据。对于 small immutable types ,这样的方式挺好的,但是对于非常大的数据结构,copy数据的开销会非常大。可以通过手动添加warper代码的方式来避免这样的开销,需要花一些精力,这方面更多细节见 Making opaque types section.

List of all builtin conversions

如下数据类型可以直接进行数据类型转换。如需转换其他数据类型,见binding Object-oriented code.

Data typeDescriptionHeader file
int8_t, uint8_t8-bit integerspybind11/pybind11.h
int16_t, uint16_t16-bit integerspybind11/pybind11.h
int32_t, uint32_t32-bit integerspybind11/pybind11.h
int64_t, uint64_t64-bit integerspybind11/pybind11.h
ssize_t, size_tPlatform-dependent sizepybind11/pybind11.h
float, doubleFloating point typespybind11/pybind11.h
boolTwo-state Boolean typepybind11/pybind11.h
charCharacter literalpybind11/pybind11.h
char16_tUTF-16 character literalpybind11/pybind11.h
char32_tUTF-32 character literalpybind11/pybind11.h
wchar_tWide character literalpybind11/pybind11.h
const char *UTF-8 string literalpybind11/pybind11.h
const char16_t *UTF-16 string literalpybind11/pybind11.h
const char32_t *UTF-32 string literalpybind11/pybind11.h
const wchar_t *Wide string literalpybind11/pybind11.h
std::stringSTL dynamic UTF-8 stringpybind11/pybind11.h
std::u16stringSTL dynamic UTF-16 stringpybind11/pybind11.h
std::u32stringSTL dynamic UTF-32 stringpybind11/pybind11.h
std::wstringSTL dynamic wide stringpybind11/pybind11.h
std::string_view, std::u16string_view, etc.STL C++17 string viewspybind11/pybind11.h
std::pair<T1, T2>Pair of two custom typespybind11/pybind11.h
std::tuple<…>Arbitrary tuple of typespybind11/pybind11.h
std::reference_wrapper<…>Reference type wrapperpybind11/pybind11.h
std::complexComplex numberspybind11/complex.h
std::array<T, Size>STL static arraypybind11/stl.h
std::vector STLdynamic arraypybind11/stl.h
std::deque STLdouble-ended queuepybind11/stl.h
std::valarraySTL value arraypybind11/stl.h
std::listSTL linked listpybind11/stl.h
std::map<T1, T2>STL ordered mappybind11/stl.h
std::unordered_map<T1, T2>STL unordered mappybind11/stl.h
std::setSTL ordered setpybind11/stl.h
std::unordered_setSTL unordered setpybind11/stl.h
std::optionalSTL optional type (C++17)pybind11/stl.h
std::experimental::optionalSTL optional type (exp.)pybind11/stl.h
std::variant<…>Type-safe union (C++17)pybind11/stl.h
std::function<…>STL polymorphic functionpybind11/functional.h
std::chrono::duration<…>STL time durationpybind11/chrono.h
std::chrono::time_point<…>STL date/timepybind11/chrono.h
Eigen::Matrix<…>Eigen: dense matrixpybind11/eigen.h
Eigen::Map<…>Eigen: mapped memorypybind11/eigen.h
Eigen::SparseMatrix<…>Eigen: sparse matrixpybind11/eigen.h
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值