'# -*- coding: utf-8 -*-' 也是 Python 中的注释吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41680533/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:26:01  来源:igfitidea点击:

Is '# -*- coding: utf-8 -*-' also a comment in Python?

pythonencodingcomments

提问by ngShravil.py

As we use #for inserting comments in Python, then how does Python takes:

正如我们#在 Python 中用于插入注释一样,那么 Python 是如何使用的:

# -*- coding: utf-8 -*-

differently?

不一样?

回答by Martijn Pieters

Yes, it is also a comment. Andthe contents of that comment carry special meaning if located at the top of the file, in the first two lines.

是的,这也是一个评论。如果该注释位于文件顶部,即前两行,则该注释的内容具有特殊含义。

From the Encoding declarationsdocumentation:

编码声明文档

If a comment in the first or second line of the Python script matches the regular expression coding[=:]\s*([-\w.]+), this comment is processed as an encoding declaration; the first group of this expression names the encoding of the source code file. The encoding declaration must appear on a line of its own. If it is the second line, the first line must also be a comment-only line.

如果 Python 脚本的第一行或第二行中的coding[=:]\s*([-\w.]+)注释与正则表达式匹配,则该注释将作为编码声明进行处理;此表达式的第一组命名源代码文件的编码。编码声明必须单独出现一行。如果是第二行,则第一行也必须是仅注释行。

Note that it doesn't matter what codec should be used to read the file, as far as comments are concerned. Python would normally ignore everythingafter the #token, and in all accepted source code codecs the #, encoding declaration and line separator characters are encoded exactly the same as they are all supersets of ASCII. So all the parser has to do is read one line, scan for the special text in the comment, read another if needed, scan for the comment, then configure the parser to read data according to the given codec.

请注意,就注释而言,应该使用什么编解码器来读取文件并不重要。Python 通常会忽略标记之后的所有内容#,并且在所有接受的源代码编解码器中#,编码声明和行分隔符的编码完全相同,因为它们都是 ASCII 的超集。所以解析器所要做的就是读取一行,扫描注释中的特殊文本,如果需要,读取另一行,扫描注释,然后配置解析器根据给定的编解码器读取数据。

Given that the comment is required to be either the first or second in the file (and if it is the second line, the first line must be a comment too), this is entirely safe, as the configured codec can only make a difference to non-comment lines anyway.

鉴于注释必须是文件中的第一行或第二行(如果是第二行,第一行也必须是注释),这是完全安全的,因为配置的编解码器只能对无论如何,非注释行。

回答by Dimitris Fasarakis Hilliard

See encoding declarationsin the Python Reference Manual:

请参阅Python 参考手册中的编码声明

If a commentin the first or second lineof the Python script matches the regular expression coding[=:]\s*([-\w.]+), this commentis processed as an encoding declaration; the first group of this expression names the encoding of the source code file.

如果评论在第一或第二行的Python脚本的正则表达式匹配coding[=:]\s*([-\w.]+),这个注释被处理为一个编码声明; 此表达式的第一组命名源代码文件的编码。

(Emphasis mine)

(强调我的)

So yes, it is a comment, a special one. It is special in that the parser will try and act on it and not ignore it as it does for comments not in the first or second line. Take, for example, an unregistered encoding declaration in a sample file decl.py:

所以是的,这是一个评论,一个特别的评论。特殊之处在于解析器将尝试对其采取行动,而不是像处理不在第一行或第二行中的注释那样忽略它。以示例文件中的未注册编码声明为例decl.py

# # -*- coding: unknown-encoding -*-
print("foo")

If you try and run this, Python will try and process it, fail and complain:

如果您尝试运行它,Python 将尝试处理它,失败并抱怨:

python decl.py 
  File "decl.py", line 1
SyntaxError: encoding problem: unknown-encoding