...and #[pymethods] holds the behavior
#[pymethods]
impl Point {
#[new]
fn new(x: f64, y: f64) -> Self {
Point { x, y }
}
fn magnitude(&self) -> f64 {
(self.x * self.x + self.y * self.y).sqrt()
}
}
#[new] marks __init__; &self is self, borrowed rather than owned.
A __repr__ method (elided here) wires straight into the Python protocol:
>>> p = pyo3_example.Point(3.0, 4.0)
>>> p.magnitude()
5.0
>>> p
Point(x=3.0, y=4.0)