How to get reference of an inner object in python? #3020
-
#[pyclass]
struct Parent {
child: Child,
}
#[pymethods]
impl Parent {
#[new]
fn new() -> Self {
Parent {
child: Child { value: 0 },
}
}
fn set_age_by_parent(&mut self, age: i32) {
self.child.set_age(age);
}
}
#[pyclass]
struct Child {
value: i32,
}
#[pymethods]
impl Child {
fn set_age(&mut self, age: i32) {
self.value = age;
}
} After wrap these code and generate py module, I could make a new |
Beta Was this translation helpful? Give feedback.
Answered by
davidhewitt
Mar 7, 2023
Replies: 1 comment
-
You should write #[pyclass]
struct Parent {
child: Py<Child>,
} i.e. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sun-rs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should write
i.e.
Parent
will store a Python reference to aChild
object, instead of just the Rust struct.