Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You return <code>bytes</code> from your <code>__unicode__</code> method.</p> <p>To make it clear:</p> <pre><code>In [18]: class Test(object): def __unicode__(self): return u'äö↓'.encode('utf-8') def __str__(self): return unicode(self) ....: In [19]: class Test2(object): def __unicode__(self): return u'äö↓' def __str__(self): return unicode(self) ....: In [20]: t = Test() In [21]: t.__str__() --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) /home/dav1d/&lt;ipython-input-21-e2650f29e6ea&gt; in &lt;module&gt;() ----&gt; 1 t.__str__() /home/dav1d/&lt;ipython-input-18-8bc639cbc442&gt; in __str__(self) 3 return u'äö↓'.encode('utf-8') 4 def __str__(self): ----&gt; 5 return unicode(self) 6 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) In [22]: unicode(t) --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) /home/dav1d/&lt;ipython-input-22-716c041af66e&gt; in &lt;module&gt;() ----&gt; 1 unicode(t) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) In [23]: t2 = Test2() In [24]: t2.__str__() Out[24]: u'\xe4\xf6\u2193' In [25]: str(_) # _ = last result --------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) /home/dav1d/&lt;ipython-input-25-3a1a0b74e31d&gt; in &lt;module&gt;() ----&gt; 1 str(_) # _ = last result UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)' In [26]: unicode(t2) Out[26]: u'\xe4\xf6\u2193' In [27]: class Test3(object): def __unicode__(self): return u'äö↓' def __str__(self): return unicode(self).encode('utf-8') ....: In [28]: t3 = Test3() In [29]: t3.__unicode__() Out[29]: u'\xe4\xf6\u2193' In [30]: t3.__str__() Out[30]: '\xc3\xa4\xc3\xb6\xe2\x86\x93' In [31]: print t3 äö↓ In [32]: print unicode(t3) äö↓ </code></pre> <p><code>print a</code> or in my case <code>print t</code> will call <code>t.__str__</code> which es expected to return <code>bytes</code> you let it return <code>unicode</code> so it tries to encode it with <code>ascii</code> which will not work.</p> <p>Easy fix: let <code>__unicode__</code> return unicode and <code>__str__</code> bytes.</p>
 

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