Skip to content

Commit

Permalink
docs: update Python modules section of the guide (#3924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Mar 2, 2024
1 parent d948277 commit 81be11e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down

0 comments on commit 81be11e

Please sign in to comment.