Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unless you specifically act to ensure otherwise, most classes don't have helpful results for either:</p> <pre><code>&gt;&gt;&gt; class Sic(object): pass ... &gt;&gt;&gt; print str(Sic()) &lt;__main__.Sic object at 0x8b7d0&gt; &gt;&gt;&gt; print repr(Sic()) &lt;__main__.Sic object at 0x8b7d0&gt; &gt;&gt;&gt; </code></pre> <p>As you see -- no difference, and no info beyond the class and object's <code>id</code>. If you only override one of the two...:</p> <pre><code>&gt;&gt;&gt; class Sic(object): ... def __repr__(object): return 'foo' ... &gt;&gt;&gt; print str(Sic()) foo &gt;&gt;&gt; print repr(Sic()) foo &gt;&gt;&gt; class Sic(object): ... def __str__(object): return 'foo' ... &gt;&gt;&gt; print str(Sic()) foo &gt;&gt;&gt; print repr(Sic()) &lt;__main__.Sic object at 0x2617f0&gt; &gt;&gt;&gt; </code></pre> <p>as you see, if you override <code>__repr__</code>, that's ALSO used for <code>__str__</code>, but not vice versa.</p> <p>Other crucial tidbits to know: <code>__str__</code> on a built-on container uses the <code>__repr__</code>, NOT the <code>__str__</code>, for the items it contains. And, despite the words on the subject found in typical docs, hardly anybody bothers making the <code>__repr__</code> of objects be a string that <code>eval</code> may use to build an equal object (it's just too hard, AND not knowing how the relevant module was actually imported makes it actually flat out impossible).</p> <p>So, my advice: focus on making <code>__str__</code> reasonably human-readable, and <code>__repr__</code> as unambiguous as you possibly can, even if that interferes with the fuzzy unattainable goal of making <code>__repr__</code>'s returned value acceptable as input to <code>__eval__</code>!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload