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,23 +66,23 @@ 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")?)
135
+
use pyo3::prelude::*;
136
+
137
+
#[pyclass]
138
+
struct Nested;
147
139
}
148
140
}
149
141
# }
150
142
```
151
143
152
-
The`#[pymodule]` macro automatically sets the `module` attribute of the `#[pyclass]` macros declared inside of it with its name.
144
+
In this case,`#[pymodule]` macro automatically sets the `module` attribute of the `#[pyclass]` macros declared inside of it with its name.
153
145
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.
146
+
In the previous example, the `Nested` class will have for `module``my_extension.submodule`.
156
147
157
-
```rust,no_run
158
-
# mod declarative_module_module_attr_test {
159
-
use pyo3::prelude::*;
148
+
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
149
161
-
#[pyclass]
162
-
struct Ext;
150
+
## Procedural initialization
163
151
164
-
#[pymodule]
152
+
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
167
+
m.add("double2", m.getattr("double")?)
178
168
}
179
169
}
180
170
# }
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