简体   繁体   English

pybind11:为“模块构造函数”添加回调函数

[英]pybind11: add callback function for "module constructor"

The pybind11 documentation has a section about "module destructors". pybind11 文档有一节关于“模块析构函数”。 How do you implement "module constructors" with pybind11?你如何用 pybind11 实现“​​模块构造函数”?

Let's say I want to execute a C++ function when my module is imported in Python.假设我想在我的模块导入 Python 时执行 C++ 函数。 Normally, if this was a Python module, I would just add the command with function call into the top-level scope in module.py .通常,如果这是一个 Python 模块,我只需将带有函数调用的命令添加到module.py的顶级范围中。 This is the most basic thing you can do in a Python module - execute a command.这是你可以在 Python 模块中做的最基本的事情——执行命令。 But pybind11's first steps start with adding a function definition .但是 pybind11 的第一步是从添加函数定义开始 How do I add a command to the pybind11 module?如何向 pybind11 模块添加命令

That's what the contents of the function defined with the PYBIND11_MODULE macro is for.这就是用PYBIND11_MODULE定义的函数的内容。 It's a regular function, you can do whatever setup you need in there;这是一个常规功能,您可以在其中进行所需的任何设置; sure, calls on m (like m.def ) are what you commonly see, but you can do anything you like in it to set up your module.当然,调用m (如m.def )是您经常看到的,但是您可以在其中执行任何您喜欢的操作来设置您的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM