Python cannot change values in an internal/nested struct #1974
-
Bug DescriptionUsing the The same issue is present if using a function (in the inner class) to get/set the variable. I suspect that python is cloning the inner object and throwing it away. Steps to ReproduceConsider the Rust structs: #[pyclass]
struct PainterCore {
#[pyo3(get, set)]
pub test1: f32,
#[pyo3(get, set)]
pub inner: InnerStruct,
}
#[pymethods]
impl PainterCore {
#[new]
pub fn new() -> PyResult<Self> {
Ok(Self {
test1: 0.0,
inner: InnerStruct{
test2: 0.0
}
})
}
}
#[pyclass]
#[derive(Clone)]
struct InnerStruct {
#[pyo3(get, set)]
test2: f32,
} And consider the python: self.core = painter_core.PainterCore()
print(self.core.test1, self.core.inner.test2)
self.core.test1 = 5.0
self.core.inner.test2 = 5.0
print(self.core.test1, self.core.inner.test2) The expected output from the first print is: The actual output of the second print is BacktraceNo response Your operating system and versionArchlinux Your Python version (
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The attribute access If you need to return a reference instead, the struct member has to be |
Beta Was this translation helpful? Give feedback.
-
See also https://pyo3.rs/v0.15.0/faq.html#pyo3get-clones-my-field |
Beta Was this translation helpful? Give feedback.
The attribute access
self.core.inner
returns a copy of the object; any changes you make will not be reflected there.If you need to return a reference instead, the struct member has to be
Py<InnerStruct>
.