I am using the pyo3
rust crate (version 0.11.1
) in order to port rust code, into cpython (version 3.8.2
) code. I have created a class called my_class
and defined the following functions: new
, __str__
, and __repr__
.
TL;DR: The
__str__
function exists on a class ported from rust using the pyo3 crate, but doesn't get printed when just usingprint(obj)
, and instead having to writeprint(obj.__str__())
The my_class
definition is here:
use pyo3::prelude::*;
#[pyclass]
struct my_class {
#[pyo3(get, set)]
num: i32,
#[pyo3(get, set)]
debug: bool,
}
#[pymethods]
impl my_class {
#[new]
fn new(num: i32, debug: bool) -> Self {
my_class {num, debug}
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("[__str__] Num: {}, Debug: {}", self.num, self.debug))
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("[__repr__] Num: {}, Debug: {}", self.num, self.debug))
}
}
#[pymodule]
fn pymspdb(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<my_class>()?;
Ok(())
}
I build this (into release mode), and test the code with the following code:
from my_module import my_class
def main():
dsa = my_class(1, True)
print(dsa)
print(dsa.__str__())
if __name__ == "__main__":
main()
When running the test python code, I get the following output:
<my_class object at 0x7fb7828ae950>
[__str__] Num: 1, Debug: true
Now I have thought of possible solutions to this. One solution might be the pyo3 rust crate actually acts as a proxy, and in order to port classes into python might implement some sort of object which transfers all actions over to the ported class. So it might not implement its own __str__
therefore not giving me what I want.
The second possible solution I thought of was I might not be overloading the __str__
function properly, therefore when python tries to use the print function it doesn't access the correct function and just does the default behavior.
Thanks for reading so far, hope I can find an answer since I didn't find anything online for this.