我应该在 Python 3 中使用编码声明吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14083111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I use encoding declaration in Python 3?
提问by Mateusz Jagie??o
Python 3 uses UTF-8 encoding for source-code files by default. Should I still use the encoding declaration at the beginning of every source file? Like # -*- coding: utf-8 -*-
默认情况下,Python 3 对源代码文件使用 UTF-8 编码。我还应该在每个源文件的开头使用编码声明吗?喜欢# -*- coding: utf-8 -*-
采纳答案by Martijn Pieters
Because the default isUTF-8, you only need to use that declaration when you deviate from the default, or if you rely on other tools (like your IDE or text editor) to make use of that information.
因为默认值是UTF-8,所以您只需要在偏离默认值时使用该声明,或者如果您依赖其他工具(如您的 IDE 或文本编辑器)来使用该信息。
In other words, as far as Python is concerned, only when you want to use an encoding that differs do you have to use that declaration.
换句话说,就 Python 而言,只有当您想使用不同的编码时,才必须使用该声明。
Other tools, such as your editor, can support similar syntax, which is why the PEP 263 specificationallows for considerable flexibility in the syntax (it must be a comment, the text codingmust be there, followed by either a :or =character and optional whitespace, followed by a recognised codec).
其他工具,例如您的编辑器,可以支持类似的语法,这就是为什么PEP 263 规范允许在语法方面具有相当大的灵活性(它必须是注释,文本coding必须在那里,后跟 a:或=字符和可选的空格,后跟一个公认的编解码器)。
Note that it only applies to how Python reads the source code. It doesn't apply to executing that code, so not to how printing, opening files, or any other I/O operations translate between bytes and Unicode. For more details on Python, Unicode, and encodings, I strongly urge you to read the Python Unicode HOWTO, or the very thorough Pragmatic Unicodetalkby Ned Batchelder.
请注意,它仅适用于 Python读取源代码的方式。它不适用于执行该代码,因此不适用于打印、打开文件或任何其他 I/O 操作在字节和 Unicode 之间转换的方式。有关 Python、Unicode 和编码的更多详细信息,我强烈建议您阅读Python Unicode HOWTO或Ned Batchelder非常彻底的Pragmatic Unicode演讲。
回答by S?awomir Lenart
No, if:
不,如果:
- entire project use only the
UTF-8, which is a default. - and you're sure your IDE tool doesn't need that encoding declaration in each file.
- 整个项目仅使用
UTF-8,这是默认设置。 - 并且您确定您的 IDE 工具不需要每个文件中的编码声明。
Yes, if
是的,如果
- your project relies on different encoding
- or relies on many encodings.
- 您的项目依赖于不同的编码
- 或依赖于许多编码。
For multi-encodings projects:
对于多编码项目:
If some files are encoded in the
non-utf-8, then even for these encoded inUTF-8you should add encoding declaration too, because the golden rule isExplicit is better than implicit.
如果某些文件在编码
non-utf-8,那么即使这些编码中UTF-8,你应该添加太多的编码声明,因为金科玉律Explicit is better than implicit.
Reference:
参考:
- PyCharm doesn't need that declaration:
- PyCharm 不需要该声明:
configuring encoding for specific file in pycharm
- vim doesn't need that declaration, but:
- vim 不需要那个声明,但是:
# vim: set fileencoding=<encoding name> :
# vim: set fileencoding=<encoding name> :

