Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>decode</code> method of unicode strings really doesn't have any applications at all (unless you have some non-text data in a unicode string for some reason -- see below). It is mainly there for historical reasons, i think. In Python 3 it is completely gone.</p> <p><code>unicode().decode()</code> will perform an implicit <em>encoding</em> of <code>s</code> using the default (ascii) codec. Verify this like so:</p> <pre><code>&gt;&gt;&gt; s = u'ö' &gt;&gt;&gt; s.decode() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 0: ordinal not in range(128) &gt;&gt;&gt; s.encode('ascii') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 0: ordinal not in range(128) </code></pre> <p>The error messages are exactly the same.</p> <p>For <code>str().encode()</code> it's the other way around -- it attempts an implicit <em>decoding</em> of <code>s</code> with the default encoding:</p> <pre><code>&gt;&gt;&gt; s = 'ö' &gt;&gt;&gt; s.decode('utf-8') u'\xf6' &gt;&gt;&gt; s.encode() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) </code></pre> <p>Used like this, <code>str().encode()</code> is also superfluous.</p> <p><strong>But</strong> there is another application of the latter method that is useful: there are <a href="http://docs.python.org/library/codecs.html#standard-encodings" rel="noreferrer">encodings</a> that have nothing to do with character sets, and thus can be applied to 8-bit strings in a meaningful way:</p> <pre><code>&gt;&gt;&gt; s.encode('zip') 'x\x9c;\xbc\r\x00\x02&gt;\x01z' </code></pre> <p>You are right, though: the ambiguous usage of "encoding" for both these applications is... awkard. Again, with separate <code>byte</code> and <code>string</code> types in Python 3, this is no longer an issue.</p>
    singulars
    1. This table or related slice is empty.
    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. 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