docs: demonstrate `NotImplemented` in `__richcmp__`
This commit is contained in:
parent
f574168974
commit
ef8ccc0deb
|
@ -174,6 +174,30 @@ impl Number {
|
|||
It checks that the `std::cmp::Ordering` obtained from Rust's `Ord` matches
|
||||
the given `CompareOp`.
|
||||
|
||||
Alternatively, if you want to leave some operations unimplemented, you can
|
||||
return `py.NotImplemented()` for some of the operations:
|
||||
|
||||
|
||||
```rust
|
||||
use pyo3::class::basic::CompareOp;
|
||||
|
||||
# use pyo3::prelude::*;
|
||||
#
|
||||
# #[pyclass]
|
||||
# struct Number(i32);
|
||||
#
|
||||
#[pymethods]
|
||||
impl Number {
|
||||
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyObject {
|
||||
match op {
|
||||
CompareOp::Eq => (self.0 == other.0).into_py(py),
|
||||
CompareOp::Ne => (self.0 != other.0).into_py(py),
|
||||
_ => py.NotImplemented(),
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Truthyness
|
||||
|
||||
We'll consider `Number` to be `True` if it is nonzero:
|
||||
|
|
Loading…
Reference in New Issue