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 +54,7 @@ To import the module, either:
48
54
49
55
## Documentation
50
56
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.
57
+
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
58
54
59
For example, building off of the above code, this will print `This module is implemented in Rust.`:
55
60
@@ -61,75 +66,54 @@ print(my_extension.__doc__)
61
66
62
67
## Python submodules
63
68
64
-
You can create a module hierarchy within a single extension module by using
// Arbitrary code to run at the module initialization
146
-
m.add("double2", m.getattr("double")?)
125
+
use pyo3::prelude::*;
126
+
127
+
#[pyclass]
128
+
struct Nested;
147
129
}
148
130
}
149
131
# }
150
132
```
151
133
152
-
The`#[pymodule]` macro automatically sets the `module` attribute of the `#[pyclass]` macros declared inside of it with its name.
134
+
In this case,`#[pymodule]` macro automatically sets the `module` attribute of the `#[pyclass]` macros declared inside of it with its name.
153
135
For nested modules, the name of the parent module is automatically added.
154
-
In the following example, the `Unit` class will have for `module``my_extension.submodule` because it is properly nested
155
-
but the `Ext` class will have for `module` the default `builtins` because it not nested.
136
+
In the previous example, the `Nested` class will have for `module``my_extension.submodule`.
156
137
157
-
```rust,no_run
158
-
# mod declarative_module_module_attr_test {
159
-
use pyo3::prelude::*;
138
+
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.
160
139
161
-
#[pyclass]
162
-
struct Ext;
140
+
## Procedural initialization
163
141
164
-
#[pymodule]
142
+
If the macros provided by PyO3 are not enough, it is possible to run code at the module initialization:
// Arbitrary code to run at the module initialization
157
+
m.add("double2", m.getattr("double")?)
178
158
}
179
159
}
180
160
# }
181
-
```
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