从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解释器是否仍会“神奇地”接受编码定义?如果答案解释了为什么它必须在第一两行并且指向解释器代码的指针会很棒,那就太好了!