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
Adds a #[deleter] macro
When deleter is set, the `PyGetSetDef.setter` function calls the setter or deleter depending on if the value is `NULL` or not
Issue #5686
@@ -620,7 +621,7 @@ Here, the `args` and `kwargs` allow creating instances of the subclass passing i
620
621
PyO3 supports two ways to add properties to your `#[pyclass]`:
621
622
622
623
- For simple struct fields with no side effects, a `#[pyo3(get, set)]` attribute can be added directly to the field definition in the `#[pyclass]`.
623
-
- For properties which require computation you can define `#[getter]`and `#[setter]` functions in the [`#[pymethods]`](#instance-methods) block.
624
+
- For properties which require computation you can define `#[getter]`, `#[setter]`and `#[deleter]` functions in the [`#[pymethods]`](#instance-methods) block.
624
625
625
626
We'll cover each of these in the following sections.
626
627
@@ -670,13 +671,43 @@ impl MyClass {
670
671
fnnum(&self) ->PyResult<i32> {
671
672
Ok(self.num)
672
673
}
674
+
675
+
#[setter]
676
+
fnset_num(&mutself, num:i32) {
677
+
self.num =num;
678
+
}
679
+
}
680
+
```
681
+
682
+
The `#[deleter]` attribute is also available to delete the property.
683
+
This corresponds to the `del my_object.my_property` python operation.
684
+
685
+
```rust
686
+
# usepyo3::prelude::*;
687
+
# usepyo3::exceptions::PyAttributeError;
688
+
#[pyclass]
689
+
structMyClass {
690
+
num:Option<i32>,
691
+
}
692
+
693
+
#[pymethods]
694
+
implMyClass {
695
+
#[getter]
696
+
fnnum(&self) ->PyResult<i32> {
697
+
self.num.ok_or_else(||PyAttributeError::new_err("num has been deleted"))
698
+
}
699
+
700
+
#[deleter]
701
+
fndelete_num(&mutself) {
702
+
self.num =None;
703
+
}
673
704
}
674
705
```
675
706
676
-
A getteror setter's function name is used as the property name by default.
707
+
A getter, setter or deleters's function name is used as the property name by default.
677
708
There are several ways how to override the name.
678
709
679
-
If a function name starts with `get_`or `set_` for getteror setter respectively, the descriptor name becomes the function name with this prefix removed.
710
+
If a function name starts with `get_`, `set_`or `delete_` for getter, setter or deleter, respectively, the descriptor name becomes the function name with this prefix removed.
680
711
This is also useful in case of Rust keywords like `type` ([raw identifiers](https://doc.rust-lang.org/edition-guide/rust-2018/module-system/raw-identifiers.html) can be used since Rust 2018).
681
712
682
713
```rust
@@ -702,7 +733,7 @@ impl MyClass {
702
733
703
734
In this case, a property `num` is defined and available from Python code as `self.num`.
704
735
705
-
Both the `#[getter]`and `#[setter]` attributes accept one parameter.
736
+
The `#[getter]`, `#[setter]`and `#[deleter]` attributes accept one parameter.
706
737
If this parameter is specified, it is used as the property name, i.e.
707
738
708
739
```rust
@@ -728,9 +759,6 @@ impl MyClass {
728
759
729
760
In this case, the property `number` is defined and available from Python code as `self.number`.
730
761
731
-
Attributes defined by `#[setter]` or `#[pyo3(set)]` will always raise `AttributeError` on `del` operations.
732
-
Support for defining custom `del` behavior is tracked in [#1778](https://github.com/PyO3/pyo3/issues/1778).
733
-
734
762
## Instance methods
735
763
736
764
To define a Python compatible method, an `impl` block for your struct has to be annotated with the `#[pymethods]` attribute.
0 commit comments