V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
paicha
V2EX  ›  Python

# -*- coding:utf-8 -*- 为什么要这样的格式?

  •  
  •   paicha ·
    paicha · 2013-07-15 22:14:19 +08:00 · 13608 次点击
    这是一个创建于 3938 天前的主题,其中的信息可能已经有所发展或是发生改变。
    「 -*- 」有什么意义么?

    同时发现这样写也是正常运行的:
    # coding:utf-8
    这样不是更简单么?
    17 条回复    1970-01-01 08:00:00 +08:00
    pubby
        1
    pubby  
       2013-07-15 22:23:34 +08:00   ❤️ 1
    emacs会处理首行的这些“设置”信息

    最常见的就是这个编码设定

    格式就是在文件首行写上:
    -*- ......... -*-
    pubby
        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: */
    paicha
        3
    paicha  
    OP
       2013-07-15 22:29:25 +08:00
    @pubby 噢噢,那么用 # -*- coding:utf-8 -*- 可以理解为比 # coding:utf-8 兼容性更好么?
    keakon
        4
    keakon  
       2013-07-15 22:30:02 +08:00
    那是 emacs 设置 encoding 的风格。
    http://www.python.org/dev/peps/pep-0263/
    pubby
        5
    pubby  
       2013-07-15 22:36:17 +08:00
    sinxccc
        6
    sinxccc  
       2013-07-15 22:38:05 +08:00
    这是给 Emacs 看的。
    paicha
        7
    paicha  
    OP
       2013-07-15 22:47:01 +08:00
    @pubby
    @sinxccc

    新手学编程勿怪。学习的教程是:Learn Python The Hard Way

    查了下,Emacs 是一种编辑器。我现在是在 gedit 里编辑,然后终端运行 python ex1.py
    然后发现中文会报错,后来就了解到声明 utf-8 编码就可以了。
    发现在其他人写的一些代码也发现有这样的声明,于是就对这个声明有了点疑问。
    notedit
        8
    notedit  
       2013-07-16 02:13:50 +08:00   ❤️ 1
    这个是python的规范 不是给emacs看的
    python的解释器会读取这个设置
    013231
        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.
    reus
        10
    reus  
       2013-07-16 05:20:10 +08:00 via Android   ❤️ 1
    解释器可以理解多种格式的声明,写成这样是顺便给编辑器看。我不用emacs所以只用最短的那种
    wildog
        11
    wildog  
       2013-07-16 05:59:54 +08:00 via Android   ❤️ 3
    没有人觉得这种声明格式很呆萌吗
    thedevil5032
        12
    thedevil5032  
       2013-07-16 07:18:24 +08:00 via iPad
    这种申明只在 Python 2 中使用.
    lisposter
        13
    lisposter  
       2013-07-16 12:39:59 +08:00
    我以为是卖萌来着。。。弱弱溜走
    davepkxxx
        14
    davepkxxx  
       2013-07-16 12:49:45 +08:00
    颜文字?
    xingzhi
        15
    xingzhi  
       2013-07-16 17:59:27 +08:00
    @thedevil5032 python3 只允许 #coding:utf-8 这样的形式?
    thedevil5032
        16
    thedevil5032  
       2013-07-16 19:29:11 +08:00
    @xingzhi Python3 不需要这样的东西, 因为 Python3 的 UTF8支持. 如果我没记错的话.
    timonwong
        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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1141 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 18:37 · PVG 02:37 · LAX 11:37 · JFK 14:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.