Defining Python modules from C++ with classes, functions, and enums.
This module provides helper classes and macros to define Python modules that can contain C++ classes, functions, enums, and other objects exported to Python.
A Python module can contain:
But every module must also be declared, and have an entrypoint.
To create a Python module, you typically use macros that work with ModuleDefinition: Macro names follow a consistent grammar, see Python Macro Name Conventions.
The ModuleDefinition class is typically not used directly. Instead, use the provided macros in pyobject_macros.h that work with ModuleDefinition instances.
Topics | |
| Module Declaration Macros | |
| These macros declare and define ModuleDefinition objects that represent Python modules. | |
| Module Entrypoint and Injection Macros | |
| These macros handle module initialization, injection, and extension module creation. | |
| Module Constants and Objects Macros | |
| These macros add constants, variables, and arbitrary Python objects to modules. | |
| Module Function Macros | |
| These macros export C++ functions to a Python and add them to a Python module. | |
| Function Cast Export Macros (Deprecated) | |
Classes | |
| class | lass::python::ModuleDefinition |
| Definition of a Python module. More... | |
Class Integration | |
Macros to add Python classes to modules. These macros integrate class definitions with module definitions, making the classes available as types within the module namespace. Classes must first be declared using | |
| #define | PY_INJECT_CLASS_IN_MODULE(t_cppClass, i_module, s_doc) |
| Inject a class into a module at runtime. | |
| #define | PY_MODULE_CLASS(i_module, t_cppClass) |
| Add a Python class to a module. | |
| #define PY_INJECT_CLASS_IN_MODULE | ( | t_cppClass, | |
| i_module, | |||
| s_doc ) |
Inject a class into a module at runtime.
This is the legacy runtime approach for adding classes to modules. Use PY_MODULE_CLASS() instead for compile-time registration.
| t_cppClass | Python binding class type that has been declared with PY_DECLARE_CLASS_* |
| i_module | Module object identifier to inject the class into |
| s_doc | Optional class documentation string (or nullptr) |
Definition at line 3952 of file pyobject_macros.h.
| #define PY_MODULE_CLASS | ( | i_module, | |
| t_cppClass ) |
Add a Python class to a module.
Registers a class definition to be included in the module when it is created. The class must have been declared with PY_DECLARE_CLASS_* macros. This is executed before main(), so the class is available when the module is created.
| i_module | Module identifier declared by PY_DECLARE_MODULE_* (must be unscoped identifier for token concatenation) |
| t_cppClass | Python binding class type that has been declared with PY_DECLARE_CLASS_* |
Definition at line 3973 of file pyobject_macros.h.