@@ -18,12 +18,13 @@ struct MyClass {
18
18
The above example generates implementations for ` PyTypeInfo ` , ` PyTypeObject `
19
19
and ` PyClass ` for ` MyClass ` .
20
20
21
- Specifically, the following implementation is generated.
21
+ Specifically, the following implementation is generated:
22
22
23
23
``` rust
24
24
use pyo3 :: prelude :: * ;
25
25
use pyo3 :: {PyClassShell , PyTypeInfo };
26
26
27
+ /// Class for demonstration
27
28
struct MyClass {
28
29
num : i32 ,
29
30
debug : bool ,
@@ -38,7 +39,7 @@ impl PyTypeInfo for MyClass {
38
39
39
40
const NAME : & 'static str = " MyClass" ;
40
41
const MODULE : Option <& 'static str > = None ;
41
- const DESCRIPTION : & 'static str = " This is a demo class " ;
42
+ const DESCRIPTION : & 'static str = " Class for demonstration " ;
42
43
const FLAGS : usize = 0 ;
43
44
44
45
#[inline]
@@ -89,14 +90,14 @@ pyo3::inventory::collect!(MyClassGeneratedPyo3Inventory);
89
90
## Get Python objects from ` pyclass `
90
91
You sometimes need to convert your ` pyclass ` into a Python object in Rust code (e.g., for testing it).
91
92
92
- For getting * GIL-bounded* (i.e., with ` 'py ` lifetime) references of ` pyclass ` ,
93
+ For getting * GIL-bounded* (i.e., with ` 'py ` lifetime) references of ` pyclass ` ,
93
94
you can use ` PyClassShell<T> ` .
94
95
Or you can use ` Py<T> ` directly, for * not-GIL-bounded* references.
95
96
96
97
### ` PyClassShell `
97
98
` PyClassShell ` represents the actual layout of ` pyclass ` on the Python heap.
98
99
99
- If you want to instantiate ` pyclass ` in Python and get the the reference,
100
+ If you want to instantiate ` pyclass ` in Python and get the reference,
100
101
you can use ` PyClassShell::new_ref ` or ` PyClassShell::new_mut ` .
101
102
102
103
``` rust
@@ -182,21 +183,24 @@ impl MyClass {
182
183
}
183
184
```
184
185
185
- Rules for the ` new ` method:
186
-
187
- * If no method marked with ` #[new] ` is declared, object instances can only be created
188
- from Rust, but not from Python.
189
- * All parameters are from Python.
190
- * It can return one of these types:
191
- - ` T `
192
- - ` PyResult<T> `
193
- - ` PyClassInitializer<T> `
194
- - ` PyResult<PyClassInitializer<T>> `
195
- * If you pyclass declared with ` #[pyclass(extends=BaseType)] ` and ` BaseType `
196
- is also ` #[pyclass] ` , you have to return ` PyClassInitializer<T> ` or
197
- ` PyResult<PyClassInitializer<T>> ` with the baseclass initialized. See the
198
- below Inheritance section for detail.
199
- * For details on the parameter list, see the ` Method arguments ` section below.
186
+ If no method marked with ` #[new] ` is declared, object instances can only be
187
+ created from Rust, but not from Python.
188
+
189
+ For arguments, see the ` Method arguments ` section below.
190
+
191
+ ### Return type
192
+
193
+ If your pyclass is declared with baseclass(i.e., you use ` #[pyclass(extends=...)]) ` ),
194
+ you must return a ` PyClassInitializer ` with the base class initialized.
195
+
196
+ For constructors that may fail, you should wrap the return type in a PyResult as well.
197
+ Consult the table below to determine which type your constructor should return:
198
+
199
+
200
+ | | ** Cannot fail** | ** May fail** |
201
+ | --------------------| -------------------------| -----------------------------------|
202
+ | ** No inheritance** | ` T ` | ` PyResult<T> ` |
203
+ | ** Inheritance** | ` PyClassInitializer<T> ` | ` PyResult<PyClassInitializer<T>> ` |
200
204
201
205
## Inheritance
202
206
0 commit comments