You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The name of the module must match the name of the `.so` or `.pyd`
@@ -48,8 +61,7 @@ To import the module, either:
48
61
49
62
## Documentation
50
63
51
-
The [Rust doc comments](https://doc.rust-lang.org/stable/book/ch03-04-comments.html) of the module
52
-
initialization function will be applied automatically as the Python docstring of your module.
64
+
The [Rust doc comments](https://doc.rust-lang.org/stable/book/ch03-04-comments.html) of the Rust module will be applied automatically as the Python docstring of your module.
53
65
54
66
For example, building off of the above code, this will print `This module is implemented in Rust.`:
55
67
@@ -61,75 +73,66 @@ print(my_extension.__doc__)
61
73
62
74
## Python submodules
63
75
64
-
You can create a module hierarchy within a single extension module by using
It is not necessary to add `#[pymodule]`on nested modules, which is only required on the top-level module.
117
+
You can provide the `submodule` argument to `#[pymodule()]`for modules that are not top-level modules in order for them to properly generate the `#[pyclass]``module` attribute automatically.
105
118
106
-
## Declarative modules
119
+
## Inline declaration
107
120
108
-
Another syntax based on Rust inline modules is also available to declare modules.
121
+
It is possible to declare functions, classes, sub-modules and constants inline in a module:
109
122
110
123
For example:
111
124
```rust,no_run
112
125
# mod declarative_module_test {
113
-
use pyo3::prelude::*;
114
-
115
-
#[pyfunction]
116
-
fn double(x: usize) -> usize {
117
-
x * 2
118
-
}
119
-
120
-
#[pymodule]
126
+
#[pyo3::pymodule]
121
127
mod my_extension {
122
-
use super::*;
123
-
124
-
#[pymodule_export]
125
-
use super::double; // Exports the double function as part of the module
128
+
use pyo3::prelude::*;
126
129
127
130
#[pymodule_export]
128
131
const PI: f64 = std::f64::consts::PI; // Exports PI constant as part of the module
// Arbitrary code to run at the module initialization
174
+
m.add("double2", m.getattr("double")?)
178
175
}
179
176
}
180
177
# }
181
178
```
182
-
It is possible to customize the `module` value for a `#[pymodule]` with the `#[pyo3(module = "MY_MODULE")]` option.
183
-
184
-
You can provide the `submodule` argument to `pymodule()` for modules that are not top-level modules -- it is automatically set for modules nested inside of a `#[pymodule]`.
0 commit comments