我正在使用pyo3rust crate (version 0.11.1) 以便将 rust 代码移植到 cpython (version 3.8.2) 代码中。我创建了一个名为类my_class,定义了以下功能:new,__str__,和__repr__。
TL;DR:该
__str__函数存在于使用 pyo3 crate 从 rust 移植的类上,但在使用时不会打印print(obj),而是必须编写print(obj.__str__())
该my_class定义是在这里:
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: {}", …Run Code Online (Sandbox Code Playgroud)