Jakub Klinkovský
Jakub Klinkovský

Reputation: 1362

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?

Upvotes: 1

Views: 660

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155333

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.

Upvotes: 1

Related Questions