简体   繁体   中英

pybind11: add callback function for "module constructor"

The pybind11 documentation has a section about "module destructors". How do you implement "module constructors" with pybind11?

Let's say I want to execute a C++ function when my module is imported in Python. Normally, if this was a Python module, I would just add the command with function call into the top-level scope in module.py . This is the most basic thing you can do in a Python module - execute a command. But pybind11's first steps start with adding a function definition . How do I add a command to the pybind11 module?

That's what the contents of the function defined with the PYBIND11_MODULE macro is for. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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