pybind11学习

这篇具有很好参考价值的文章主要介绍了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

#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)

$ 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

#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的内容基本相同。
文中介绍了两种方式:文章来源地址https://www.toymoban.com/news/detail-691264.html

  1. 使用标准的python方式,设置setuptools的相关参数;
  2. 使用cmake程序,设置setuptools的相关参数;
    感觉第一种方式更加简洁一些,但是是不存在什么不足,没有深入研究。

到了这里,关于pybind11学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from mn

    树莓派4B在使用onnxruntime1.6.0对model.onnx模型进行加载的时候出现以下的报错: 原因: 由于导出的ONNX模型文件不兼容ONNX Runtime当前版本所导致的,一开始我导出模型的方式如下: 然后会得到一个文件夹: 接着我在上面生成的文件夹的那个路径中打开树莓派的黑窗口,然后输入

    2024年02月11日
    浏览(26)
  • 【Python学习】Python学习11-元组

    目录 文章所属专区 Python学习 本章节主要说明Python的Python 的元组与列表类似,不同之处在于元组的元素不能修改。通过小括号创建。 创建一个列表 通过方括号和逗号分割创建,列表数据项中不需要有相同的类型 创建空元组 元组中只包含一个元素时,需要在元素后面添加逗

    2024年01月24日
    浏览(41)
  • 【Python机器学习】实验11 神经网络-感知器

    1.感知机是根据输入实例的特征向量 x x x 对其进行二类分类的线性分类模型: f ( x ) = sign ⁡ ( w ⋅ x + b ) f(x)=operatorname{sign}(w cdot x+b) f ( x ) = sign ( w ⋅ x + b ) 感知机模型对应于输入空间(特征空间)中的分离超平面 w ⋅ x + b = 0 w cdot x+b=0 w ⋅ x + b = 0 。 2.感知机学习的策略

    2024年02月13日
    浏览(31)
  • ChatGPT实战100例 - (11) 零成本学习Python

    用ChatGPT列一个培训大纲, 然后:哪里不会点哪里! 问题: 回答

    2024年02月06日
    浏览(28)
  • Python学习笔记(11-2):matplotlib绘图——图形绘制函数

    因为部分图形绘制函数共用了一套参数体系,在颜色、曲线形状等部分的使用方式也是一致的。所以,在讲解各类图形绘制之前,我们整体性地对各类通用参数进行一个整理,并在此基础上对于颜色(color)、数据点标记(marker)和曲线形式(linestyle)等几个通用参数进行相

    2024年02月06日
    浏览(41)
  • python学习笔记11(程序跳转语句、空语句)

    (一)程序跳转语句 1、break 用法:循环语句中使用, 结束本层 循环,一般搭配if来使用。注意while/else语法 示例: 2、continue 用法:循环语句中使用, 结束本层 本次循环,一般搭配if来使用。 (二)空语句 pass,占位符,任何地方都可以用 (三)小结

    2024年01月24日
    浏览(30)
  • Python&aconda系列:GPU深度学习环境搭建:Win11+CUDA 11.7+Pytorch1.12.1+Anaconda以及对应版本

    官方推荐的cuda版本为10.2和11.3,这两种 cuda 支持大多数的 pytorch 版本。 以下是Pytorch和CUDA对应的版本 CUDA 环境 PyTorch 版本 9.2 0.4.1、1.2.0、1.4.0、1.5.0(1)、1.6.0、1.7.0(1) 10.0 1.2.0、1.1.0、1.0.0(1) 10.1 1.4.0、1.5.0(1)、1.6.0、1.7.0(1) 10.2 1.5.0(1)、1.6.0、1.7.0(1)、1.8.0(1)、1.9.0、1.9.0、1.10.0、1.

    2024年02月02日
    浏览(59)
  • 学习记录:Windows系统cuda11.6,安装pytorch1.12.0、python3.9

    1、查看显卡相关信息:nvidia-smi。显卡版本531.18,最大可以安装cuda12.1版本,安装步骤上一篇博客讲解过。 2、查看cuda版本:nvcc -V 3、查看anaconda是否安装:conda -V 4、查询cuda11.6对应的pytorch版本:https://pytorch.org/get-started/previous-versions/ 显示对应的pytorch1.12.0、1.12.1,接着查询适

    2023年04月17日
    浏览(35)
  • 《零基础入门学习Python》第063讲:论一只爬虫的自我修养11:Scrapy框架之初窥门径

    上一节课我们好不容易装好了 Scrapy,今天我们就来学习如何用好它,有些同学可能会有些疑惑,既然我们懂得了Python编写爬虫的技巧,那要这个所谓的爬虫框架又有什么用呢?其实啊,你懂得Python写爬虫的代码,好比你懂武功,会打架,但行军打仗你不行,毕竟敌人是千军

    2024年02月16日
    浏览(33)
  • windows10系统PYthon深度学习环境安装(Anaconda3、PYthon3.10、CUDA11.6、CUDDN10、pytorch、tensorflow,Pycharm)

    一、 總體说明 1、說明:總體採用https://blog.csdn.net/zhizhuxy999/article/details/90442600方法,部分步驟由於版本變化,進行了調整。 2、基本概念 编程语言/编译器:Python。Python的特点是“用最少的代码干最多的事”。Python 2即在2020年停止更新,所以现在学习Python 3是最好的选择。 P

    2023年04月18日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包