docs: update Python modules section of the guide (#3924)
This commit is contained in:
parent
d94827720e
commit
81be11e67a
|
@ -65,22 +65,22 @@ print(my_extension.__doc__)
|
|||
## Python submodules
|
||||
|
||||
You can create a module hierarchy within a single extension module by using
|
||||
[`PyModule.add_submodule()`]({{#PYO3_DOCS_URL}}/pyo3/prelude/struct.PyModule.html#method.add_submodule).
|
||||
[`Bound<'_, PyModule>::add_submodule()`]({{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyModuleMethods.html#tymethod.add_submodule).
|
||||
For example, you could define the modules `parent_module` and `parent_module.child_module`.
|
||||
|
||||
```rust
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
register_child_module(py, m)?;
|
||||
fn parent_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
register_child_module(m)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
|
||||
let child_module = PyModule::new_bound(py, "child_module")?;
|
||||
fn register_child_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
let child_module = PyModule::new_bound(parent_module.py(), "child_module")?;
|
||||
child_module.add_function(wrap_pyfunction!(func, &child_module)?)?;
|
||||
parent_module.add_submodule(child_module.as_gil_ref())?;
|
||||
parent_module.add_submodule(&child_module)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue