Skip to content

Commit

Permalink
Update class.md - add example of new returning a PyResult (#1688)
Browse files Browse the repository at this point in the history
* Update class.md - add example of `new` returning a `PyResult`

* Update class.md
  • Loading branch information
aviramha committed Jun 23, 2021
1 parent dc66afa commit 13cd092
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ impl MyClass {
}
```

Alternatively, if your `new` method may fail you can return `PyResult<Self>`.
```rust
# use pyo3::prelude::*;
#[pyclass]
struct MyClass {
num: i32,
}

#[pymethods]
impl MyClass {
#[new]
fn new(num: i32) -> PyResult<Self> {
Ok(MyClass { num })
}
}
```

If no method marked with `#[new]` is declared, object instances can only be
created from Rust, but not from Python.

Expand Down

0 comments on commit 13cd092

Please sign in to comment.