Skip to content

Commit bccfaf4

Browse files
committed
add docs in guide
1 parent 12004e3 commit bccfaf4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

guide/pyclass-parameters.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Parameter | Description |
44
| :- | :- |
5+
| `constructor` | This is currently only allowed on [variants of complex enums][params-constructor]. It allows customization of the generated class constructor for each variant. It uses the same syntax and supports the same options as the `signature` attribute of functions and methods. |
56
| <span style="white-space: pre">`crate = "some::path"`</span> | Path to import the `pyo3` crate, if it's not accessible at `::pyo3`. |
67
| `dict` | Gives instances of this class an empty `__dict__` to store custom attributes. |
78
| <span style="white-space: pre">`extends = BaseType`</span> | Use a custom baseclass. Defaults to [`PyAny`][params-1] |
@@ -39,5 +40,6 @@ struct MyClass {}
3940
[params-4]: https://doc.rust-lang.org/std/rc/struct.Rc.html
4041
[params-5]: https://doc.rust-lang.org/std/sync/struct.Arc.html
4142
[params-6]: https://docs.python.org/3/library/weakref.html
43+
[params-constructor]: https://pyo3.rs/latest/class.html#complex-enums
4244
[params-mapping]: https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types
4345
[params-sequence]: https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types

guide/src/class.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,46 @@ Python::with_gil(|py| {
12431243
})
12441244
```
12451245

1246+
The constructor of each generated class can be customized using the `#[pyo3(constructor = (...))]` attribute. This uses the same syntax as the [`#[pyo3(signature = (...))]`](function/signature.md)
1247+
attribute on function and methods and supports the same options. To apply this attribute simply place it on top of a variant in a `#[pyclass]` complex enum as shown below:
1248+
1249+
```rust
1250+
# use pyo3::prelude::*;
1251+
#[pyclass]
1252+
enum Shape {
1253+
#[pyo3(constructor = (radius=1.0))]
1254+
Circle { radius: f64 },
1255+
#[pyo3(constructor = (*, width, height))]
1256+
Rectangle { width: f64, height: f64 },
1257+
#[pyo3(constructor = (side_count, radius=1.0))]
1258+
RegularPolygon { side_count: u32, radius: f64 },
1259+
Nothing { },
1260+
}
1261+
1262+
# #[cfg(Py_3_10)]
1263+
Python::with_gil(|py| {
1264+
let cls = py.get_type_bound::<Shape>();
1265+
pyo3::py_run!(py, cls, r#"
1266+
circle = cls.Circle()
1267+
assert isinstance(circle, cls)
1268+
assert isinstance(circle, cls.Circle)
1269+
assert circle.radius == 1.0
1270+
1271+
square = cls.Rectangle(width = 1, height = 1)
1272+
assert isinstance(square, cls)
1273+
assert isinstance(square, cls.Rectangle)
1274+
assert square.width == 1
1275+
assert square.height == 1
1276+
1277+
hexagon = cls.RegularPolygon(6)
1278+
assert isinstance(hexagon, cls)
1279+
assert isinstance(hexagon, cls.RegularPolygon)
1280+
assert hexagon.side_count == 6
1281+
assert hexagon.radius == 1
1282+
"#)
1283+
})
1284+
```
1285+
12461286
## Implementation details
12471287

12481288
The `#[pyclass]` macros rely on a lot of conditional code generation: each `#[pyclass]` can optionally have a `#[pymethods]` block.

0 commit comments

Comments
 (0)