pybind11学习

@2023.9.1

参考pybind11官方文档:https://pybind11.readthedocs.io/en/stable/index.html

参考:https://blog.csdn.net/fengbingchun/article/details/123022405

Installing the library

我是在wsl-ubuntu中进行测试;在python虚拟环境中安装:pip install pybind11

First steps

通过一个例子,说明基本的语法,文件example.cpp

c 复制代码
#include <pybind11/pybind11.h>
namespace py = pybind11;

int add(int i = 1, int j = 2)
{
    return i + j;
}

PYBIND11_MODULE(example, m)
{
    m.doc() = "pybind11 example plugin";
    m.def("add", &add, "A function that adds two numbers", py::arg("i") = 1, py::arg("j") = 2);
    // using namespace pybind11::literals;
    // m.def("add", &add, "A function that adds two numbers", "i"_a=1, "j"_a=2);

    m.attr("the_answer") = 42;
    py::object world = py::cast("World");
    m.attr("what") = world;
    // m.attr("what") = "world"
}

编译命令:c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

shell 复制代码
$ python
Python 3.9.10 (main, Jan 15 2022, 11:48:04)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.add(1, 2)
3
>>>> help(example)

....

FUNCTIONS
    add(...)
        Signature : (i: int = 1, j: int = 2) -> int

        A function which adds two numbers
>>> example.the_answer
42
>>> example.what
'World'

Object-oriented code

In C++, a type is only considered polymorphic if it has at least one virtual function and pybind11 will automatically recognize this

注意 :这会影响继承类的调用,参考https://pybind11.readthedocs.io/en/stable/classes.html#inheritance-and-automatic-downcasting

c 复制代码
#include <pybind11/pybind11.h>

namespace py = pybind11;

struct Pet
{
    Pet(const std::string &name, int age) : name(name), age(age) {}

    void setName(const std::string &name_) { name = name_; }
    const std::string &getName() const { return name; }

    void set(int age_) { age = age_; }
    void set(const std::string &name_) { name = name_; }

    std::string name;
    int age;
};

struct Dog : Pet
{
    Dog(const std::string &name, int age) : Pet(name, age) {}
    std::string bark() const { return "woof!"; }
};

PYBIND11_MODULE(example, m) {
    py::class_<Pet>(m, "Pet", py::dynamic_attr()) // py::dynamic_attr()支持动态添加属性,不推荐
        .def(py::init<const std::string &, int>())
        .def("set", static_cast<void (Pet::*)(int)>(&Pet::set), "Set the pet's age")
        .def("set", static_cast<void (Pet::*)(const std::string &)>(&Pet::set), "Set the pet's name")
        .def("setName", &Pet::setName)
        .def("getName", &Pet::getName)
        .def_readwrite("name", &Pet::name)
        .def("__repr__",
             [](const Pet &a)
             {
                 return "<example.Pet named '" + a.name + "'>";
             });

    // Method 1: template parameter:
    py::class_<Dog, Pet /* <- specify C++ parent type */>(m, "Dog")
        // Method 2: pass parent class_ object:
        // py::class_<Dog>(m, "Dog", pet /* <- specify Python parent type */)
        .def(py::init<const std::string &, int>())
        .def("bark", &Dog::bark);
}

Build systems

主要介绍如何把代码打包分享给别人使用,这部分内容和setuptools的内容基本相同。

文中介绍了两种方式:

  1. 使用标准的python方式,设置setuptools的相关参数;
  2. 使用cmake程序,设置setuptools的相关参数;
    感觉第一种方式更加简洁一些,但是是不存在什么不足,没有深入研究。
相关推荐
奈斯。zs8 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody20508 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术9 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
Narutolxy43 分钟前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
Amo Xiang1 小时前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm31 小时前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~2 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
waterHBO4 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七5 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql