A #pyclass that has another #pyclass as attribute #4585
Answered
by
davidhewitt
piotryordanov
asked this question in
Questions
-
I have created a #pyclass struct This code runs as expected, but I have some questions:
Thank you very much! use pyo3::{prelude::*, types::PyType};
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::Decimal;
#[pyclass(get_all)]
#[derive(Debug)]
pub struct Foo {
pub inner: Decimal,
}
#[pyclass(get_all, subclass)]
#[derive(Debug)]
pub struct Bar {
pub foo: Py<Foo>,
}
#[pymethods]
impl Bar {
#[new]
fn new() -> PyResult<Bar> {
Python::with_gil(|py| {
let foo: Py<Foo> = Py::new(
py,
Foo {
inner: Decimal::from_f64(24.1).unwrap(),
},
)?;
Ok(Bar { foo })
})
}
fn test(&self) {
Python::with_gil(|py| {
let val = self.foo.borrow(py);
println!("Za val is this {:?}", val);
});
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
davidhewitt
Sep 30, 2024
Replies: 1 comment 2 replies
-
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
piotryordanov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#[pyclass(frozen)]
, and then you can use.get()
instead of.borrow(py)
and don't need the GIL.