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
shsfoolish
V2EX  ›  Python

Python 怎么优雅地中文注释

  •  1
     
  •   shsfoolish · 2016-02-17 11:43:05 +08:00 · 5551 次点击
    这是一个创建于 2981 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python 2.x 字符编码是一直被吐槽的事情

    下面的中文注释会导致出错

    print "hello world"  # 第一个 python 程序
    

    简单在第一行加上# coding:utf-8可以使程序正常运行,背后的原理在此不讨论了。

    其实有经验的 pythoner 都不提倡设置coding,那么请问如果不设的话还能实现中文注释吗?

    第 1 条附言  ·  2016-02-17 13:39:16 +08:00
    - -|| python 新手,记错了(囧,(看到回复狗眼已瞎,谢谢各位(逃
    24 条回复    2016-02-18 16:03:54 +08:00
    lichun
        1
    lichun  
       2016-02-17 11:53:45 +08:00
    换 Python3
    mhycy
        2
    mhycy  
       2016-02-17 11:54:57 +08:00
    为何不推荐#coding ?
    patrickstar
        3
    patrickstar  
       2016-02-17 12:02:35 +08:00
    为何不提倡设置 coding ,一个编码方式啊
    hahastudio
        4
    hahastudio  
       2016-02-17 12:02:51 +08:00
    为什么有经验的人会不推荐 PEP0263 呢?
    https://www.python.org/dev/peps/pep-0263/
    mikicomo
        5
    mikicomo  
       2016-02-17 12:05:06 +08:00
    虽然 python3 没这问题,但还是关注下
    janxin
        6
    janxin  
       2016-02-17 12:05:10 +08:00
    其实有经验的 pythoner 都不提倡设置 coding

    哪来的有经验的 pythoner ?都是 py3 用户?
    Zzzzzzzzz
        7
    Zzzzzzzzz  
       2016-02-17 12:06:38 +08:00
    完了, 从 2.4 开始写 python 写到现在完全没经验了
    Owenjia
        8
    Owenjia  
       2016-02-17 12:14:00 +08:00
    > 其实有经验的 pythoner 都不提倡设置 coding

    理由?
    xavierskip
        9
    xavierskip  
       2016-02-17 12:25:40 +08:00   ❤️ 1
    不提倡的应该是这个`sys.setdefaultencoding('utf-8') `
    http://blog.ernest.me/post/python-setdefaultencoding-unicode-bytes
    walleL
        10
    walleL  
       2016-02-17 12:25:42 +08:00   ❤️ 1
    是不建议设置 sys.setdefaultencoding()
    Kilerd
        11
    Kilerd  
       2016-02-17 12:32:27 +08:00
    真是瞎了自己的狗眼
    monsterxx03
        12
    monsterxx03  
       2016-02-17 12:38:17 +08:00   ❤️ 2
    有经验的会告诉你不要写中文注释
    fy
        13
    fy  
       2016-02-17 12:39:57 +08:00
    推荐不设置 encoding ? py2 日子别过了。
    midtwo
        14
    midtwo  
       2016-02-17 12:45:50 +08:00
    用英文说不溜注释的我还是乖乖地加# coding:utf-8
    而且在一大片英文中用中文很显眼,区分起来也方便
    merito
        15
    merito  
       2016-02-17 13:14:57 +08:00
    我还是习惯用英文,中式英语也行啊。不然老是输入法切换,按 shift 什么的,太犯人
    iyaozhen
        16
    iyaozhen  
       2016-02-17 13:17:06 +08:00
    @xavierskip 应该是这个吧。 coding 是提倡的,不然还要不要人活了
    jixiangqd
        17
    jixiangqd  
       2016-02-17 13:22:52 +08:00   ❤️ 1
    这个是设置程序文本编码的,最好要设置的。
    sys.setdefaultencoding() 是不推荐的,否则 python 加载完也不会 del 掉这个函数
    ytmsdy
        18
    ytmsdy  
       2016-02-17 15:42:35 +08:00
    #coding:utf-8
    加上去就好了。。
    leozy2014
        19
    leozy2014  
       2016-02-17 16:06:21 +08:00
    新手,用的 python3 , utf8 编码。没注释 coding ,全中文变量。。。。还未遇到什么坑。
    WangYanjie
        20
    WangYanjie  
       2016-02-17 17:29:26 +08:00
    讲道理的话,字符串编码是坑,注释不是坑
    FindHao
        21
    FindHao  
       2016-02-17 21:53:40 +08:00
    FindHao
        22
    FindHao  
       2016-02-17 21:54:19 +08:00
    欧,,手残发错格式了。。
    [Google Python 风格指南 - 中文版]( http://zh-google-styleguide.readthedocs.org/en/latest/google-python-styleguide/contents/)
    weaming
        23
    weaming  
       2016-02-18 09:08:19 +08:00
    @FindHao 回复好像使用不了 markdown ?
    billlee
        24
    billlee  
       2016-02-18 16:03:54 +08:00 via Android
    @leozy2014 Python 3 源代码的默认编码是 UTF-8.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5513 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:06 · PVG 16:06 · LAX 01:06 · JFK 04:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.