<p>不,顶部的编解码器仅通知Python如何解释源代码,并使用该编解码器解释Unicode文本。它不会将文本bytestrings转换为unicode值。作为<a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow">PEP 263</a>状态:</p>
<blockquote>
<p>This PEP proposes to introduce a syntax to declare the encoding of
a Python source file. The encoding information is then used by the
Python parser to interpret the file using the given encoding. Most
notably <em>this enhances the interpretation of Unicode literals in
the source code</em> and makes it possible to write Unicode literals
using e.g. UTF-8 directly in an Unicode aware editor.</p>
</blockquote>
<p>强调我的。</p>
<p>如果没有编解码器声明,Python就不知道如何解释非ASCII字符:</p>
<pre><code>$ cat /tmp/test.py
example = '☃'
$ python2.7 /tmp/test.py
File "/tmp/test.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file /tmp/test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
</code></pre>
<p>如果Python的行为与您期望的一样,那么您也将无法对包含非ASCII字节值的bytestring值进行文本处理。</p>
<p>如果您的终端被配置为显示UTF-8值,那么打印UTF-8编码的字节字符串看起来是“正确的”,但这只是因为编码匹配的运气。</p>
<p>获取unicode值的正确方法是使用unicode文本或以其他方式生成unicode(从字节字符串解码、将整数码位转换为unicode字符等):</p>
<pre><code>unicode_snowman = '\xe2\x98\x83'.decode('utf8')
unicode_snowman = unichr(0x2603)
</code></pre>
<p>在Python 3中,编解码器还适用于变量名的解释方式,因为您可以在名称中使用ASCII范围之外的字母和数字。Python 3中的默认编解码器是UTF-8,而Python 2中是ASCII。</p>