title: __str__ function of class ported from rust to python using pyo3 doesn't get used in print
body: <p>I am using the <code>pyo3</code> rust crate (version <code>0.11.1</code>) in order to port rust code, into cpython (version <code>3.8.2</code>) code. I have created a class called <code>my_class</code> and defined the following functions: <code>new</code>, <code>__str__</code>, and <code>__repr__</code>.</p>
  <blockquote>
  <p><strong>TL;DR</strong>: The <code>__str__</code> function exists on a class ported from rust using the pyo3 crate, but doesn't get printed when just using <code>print(obj)</code>, and instead having to write <code>print(obj.__str__())</code></p>
  </blockquote>
  <p>The <code>my_class</code> definition is here:</p>
  <pre class="lang-rust prettyprint-override"><code>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) -&gt; Self {
          my_class {num, debug}
      }
      fn __str__(&amp;self) -&gt; PyResult&lt;String&gt;   {
          Ok(format!(&quot;[__str__] Num: {}, Debug: {}&quot;, self.num, self.debug))
      }
  
      fn __repr__(&amp;self) -&gt; PyResult&lt;String&gt; {
          Ok(format!(&quot;[__repr__] Num: {}, Debug: {}&quot;, self.num, self.debug))
      }
  }
  
  #[pymodule]
  fn pymspdb(_py: Python, m: &amp;PyModule) -&gt; PyResult&lt;()&gt; {
      m.add_class::&lt;my_class&gt;()?;
      Ok(())
  }
  </code></pre>
  <p>I build this (into release mode), and test the code with the following code:</p>
  <pre class="lang-py prettyprint-override"><code>from my_module import my_class
  
  def main():
      dsa = my_class(1, True)
      print(dsa)
      print(dsa.__str__())
      
      
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>When running the test python code, I get the following output:</p>
  <pre><code>&lt;my_class object at 0x7fb7828ae950&gt;
  [__str__] Num: 1, Debug: true
  </code></pre>
  <p>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 <code>__str__</code> therefore not giving me what I want.
  The second possible solution I thought of was I might not be overloading the <code>__str__</code> function properly, therefore when python tries to use the print function it doesn't access the correct function and just does the default behavior.</p>
  <p>Thanks for reading so far, hope I can find an answer since I didn't find anything online for this.</p>