【发布时间】:2017-03-14 04:38:10
【问题描述】:
来自PEP263:
要定义源代码编码,必须将魔术注释放在源文件中,作为文件的第一行或第二行,例如:
# coding=<encoding name>
或(使用流行编辑器认可的格式):
#!/usr/bin/python
# -*- coding: <encoding name> -*-
如果在某些情况下许可信息出现在最上面一行,例如来自https://github.com/google/seq2seq/blob/master/seq2seq/training/utils.py:
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-
"""Miscellaneous training utility functions.
"""
编码定义是否仍会被 Python 解释器“神奇地”接受?如果答案能解释为什么它必须位于第一两行并且指向解释器代码的指针会太棒了!
【问题讨论】:
-
你引用的措辞很清楚,编码必须在第一行或第二行。
-
你试过了吗?你会怎么做才能确定这个编码行是否在做它打算做的事情?
-
我们怎样才能知道编码行是否在做它打算做的事情?在代码中添加一些“utf8”字符?
-
如果不知道“你现在在做什么”,第一行和第二行对你没有任何帮助。编码有更多的变种(操作系统、系统、python 模块等),要看到神奇,需要选择限制编程的所有神奇力量!