1. The complete Python source file should use a single encoding. Embedding of differently encoded data is not allowed and will result in a decoding error during compilation of the Python source code.
Python源文件应该使用单一编码,嵌入不同编码的数据是不允许的(个人猜测:比如单一文件里一部分使用GBK,一部分使用BIG码是不行的),会导致解码错误。
Any encoding which allows processing the first two lines in the way indicated above is allowed as source code encoding, this includes ASCII compatible encodings as well as certain multi-byte encodings such as Shift_JIS. It does not include encodings which use two or more bytes for all characters like e.g. UTF-16. The reason for this is to keep the encoding detection algorithm in the tokenizer simple.
任何允许处理头两行的编码可以作为源代码编码的格式,这包括ASCII兼容编码以及某些多字节编码,比如SHIFT_JIS。它不包括为所有字符都是有双字节或者更多字节的编码,比如UTF-16(注:也就是通常说的Unicode,但SHIFT_JIS也好,GBK也好,因为兼容ASCII编码,所以都可以在Python源文件里使用)。这么做的理由是,可以使用简单的字符在编码探测算法里。
摘自这里:
http://legacy.python.org/dev/peps/pep-0263/
# -*- coding: UTF-8 –*-
#coding=utf-8
#coding:utf-8
#coding=gbk
结论:可以使用utf-8,如果涉及跨平台的时候不要带BOM,也可以采用GBK,但就是不能使用utf16。但是有意思的是,我使用PyScripter作为IDE编程,源代码里带中文注释,把Python文件转成UTF16-LE后编程和运行一点问题都没有,还有部分优势,比如能正确显示200 µs,utf8下就有乱码。但是发布的时候,可不要这样。
这篇文章不错:
http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/
http://www.jb51.net/article/26543.htm