1
pubby 2013-07-15 22:23:34 +08:00 1
emacs会处理首行的这些“设置”信息
最常见的就是这个编码设定 格式就是在文件首行写上: -*- ......... -*- |
2
pubby 2013-07-15 22:25:39 +08:00
还有一种是写在文件末尾
比如: /* ----- for emacs ----- */ /* Local Variables: */ /* mode: php */ /* coding: utf-8-unix */ /* tab-width: 4 */ /* c-basic-offset: 4 */ /* indent-tabs-mode: nil */ /* End: */ |
3
paicha OP @pubby 噢噢,那么用 # -*- coding:utf-8 -*- 可以理解为比 # coding:utf-8 兼容性更好么?
|
4
keakon 2013-07-15 22:30:02 +08:00
那是 emacs 设置 encoding 的风格。
http://www.python.org/dev/peps/pep-0263/ |
5
pubby 2013-07-15 22:36:17 +08:00
@paicha
额,不是这么说,如果你指的是emacs,那么可以看看这个 http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables |
6
sinxccc 2013-07-15 22:38:05 +08:00
这是给 Emacs 看的。
|
7
paicha OP |
8
notedit 2013-07-16 02:13:50 +08:00 1
这个是python的规范 不是给emacs看的
python的解释器会读取这个设置 |
9
013231 2013-07-16 03:26:18 +08:00 2
Python支持3種不同方式的文件編碼聲明:
http://www.python.org/dev/peps/pep-0263/ Defining the Encoding Python will default to ASCII as standard encoding if no other encoding hints are given. To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as: # coding=<encoding name> or (using formats recognized by popular editors) #!/usr/bin/python # -*- coding: <encoding name> -*- or #!/usr/bin/python # vim: set fileencoding=<encoding name> : More precisely, the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)". The first group of this expression is then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation. There must not be any Python statement on the line that contains the encoding declaration. 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. |
10
reus 2013-07-16 05:20:10 +08:00 via Android 1
解释器可以理解多种格式的声明,写成这样是顺便给编辑器看。我不用emacs所以只用最短的那种
|
11
wildog 2013-07-16 05:59:54 +08:00 via Android 3
没有人觉得这种声明格式很呆萌吗
|
12
thedevil5032 2013-07-16 07:18:24 +08:00 via iPad
这种申明只在 Python 2 中使用.
|
13
lisposter 2013-07-16 12:39:59 +08:00
我以为是卖萌来着。。。弱弱溜走
|
14
davepkxxx 2013-07-16 12:49:45 +08:00
颜文字?
|
15
xingzhi 2013-07-16 17:59:27 +08:00
@thedevil5032 python3 只允许 #coding:utf-8 这样的形式?
|
16
thedevil5032 2013-07-16 19:29:11 +08:00
@xingzhi Python3 不需要这样的东西, 因为 Python3 的 UTF8支持. 如果我没记错的话.
|
17
timonwong 2013-07-16 20:08:14 +08:00 2
@thedevil5032
@xingzhi 是可选,因为python3源文件默认编码是utf-8: http://www.python.org/dev/peps/pep-3120/ 如果源文件需要是由其它编码保存的话,仍然需要指定encoding |