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
Copy file name to clipboardExpand all lines: guide/pyclass-parameters.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
| Parameter | Description |
4
4
| :- | :- |
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. |
5
6
| <spanstyle="white-space: pre">`crate = "some::path"`</span> | Path to import the `pyo3` crate, if it's not accessible at `::pyo3`. |
6
7
|`dict`| Gives instances of this class an empty `__dict__` to store custom attributes. |
7
8
| <spanstyle="white-space: pre">`extends = BaseType`</span> | Use a custom baseclass. Defaults to [`PyAny`][params-1]|
Copy file name to clipboardExpand all lines: guide/src/class.md
+40Lines changed: 40 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1243,6 +1243,46 @@ Python::with_gil(|py| {
1243
1243
})
1244
1244
```
1245
1245
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
+
# usepyo3::prelude::*;
1251
+
#[pyclass]
1252
+
enumShape {
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
+
letcls=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
+
1246
1286
## Implementation details
1247
1287
1248
1288
The `#[pyclass]` macros rely on a lot of conditional code generation: each `#[pyclass]` can optionally have a `#[pymethods]` block.
0 commit comments