python注释码,Python编码注释格式

Originally, I've learned to specify the source code encoding in Python 2.7 this way:

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

Now I just noticed, that PEP263 also allows this:

# coding=utf-8

Is there any differences between these? What about editor compatiblity, cross-platform etc.?

What about Python 3? Is this comment still needed for python 3 or is any code in python 3 expected to be utf-8 by default?

解决方案

Take a look at PEP3120 which changed the default encoding of python source code to be UTF-8

For python 3.x one therefore finds in the docs:

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 recommended forms of an

encoding expression are:

# -*- coding: -*-

which is recognized also by GNU Emacs, and

# vim:fileencoding=

which is recognized by Bram Moolenaar’s VIM.

If no encoding declaration is found, the default encoding is UTF-8

The take home message is therefore:

python 3.x does not neccessarily need to have utf-8 specified, since it is the default

The way the coding line is written is to some degree personal choice (only a recommendation in the docs), it only has to match the regex.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值