Skip to content

Commit

Permalink
Merge pull request #3552 from daemontus/main
Browse files Browse the repository at this point in the history
docs: Example of dynamic return type in the "Python classes" guide
  • Loading branch information
adamreichold committed Oct 27, 2023
2 parents 579af5e + 2fbc02d commit dc251d0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion guide/src/class.md
Expand Up @@ -337,10 +337,27 @@ impl SubSubClass {
let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
SubClass::method2(super_).map(|x| x * v)
}

#[staticmethod]
fn factory_method(py: Python<'_>, val: usize) -> PyResult<PyObject> {
let base = PyClassInitializer::from(BaseClass::new());
let sub = base.add_subclass(SubClass { val2: val });
if val % 2 == 0 {
Ok(Py::new(py, sub)?.to_object(py))
} else {
let sub_sub = sub.add_subclass(SubSubClass { val3: val });
Ok(Py::new(py, sub_sub)?.to_object(py))
}
}
}
# Python::with_gil(|py| {
# let subsub = pyo3::PyCell::new(py, SubSubClass::new()).unwrap();
# pyo3::py_run!(py, subsub, "assert subsub.method3() == 3000")
# pyo3::py_run!(py, subsub, "assert subsub.method3() == 3000");
# let subsub = SubSubClass::factory_method(py, 2).unwrap();
# let subsubsub = SubSubClass::factory_method(py, 3).unwrap();
# let cls = py.get_type::<SubSubClass>();
# pyo3::py_run!(py, subsub cls, "assert not isinstance(subsub, cls)");
# pyo3::py_run!(py, subsubsub cls, "assert isinstance(subsubsub, cls)");
# });
```

Expand Down

0 comments on commit dc251d0

Please sign in to comment.