To aid with platforms such as Windows, which add Unicode BOM marks to the beginning of Unicode files, the UTF-8 signature '\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well (even if no magic encoding comment is given).
If a source file uses both the UTF-8 BOM mark signature and a magic encoding comment, the only allowed encoding for the comment is 'utf-8'. Any other encoding will cause an error.
RayStorm_
来源:Python官方Unicode指南
同时参考:PEP 0263
所以可以认为在UTF-8保存的Python源码并不需要加上
# -*- coding: utf-8 -*-
声明,会被默认解释成UTF-8,即使在windows平台下使用记事本保存为UTF-8(此时会插入三字节的BOM)也没问题。不过此时要注意: