從PEP263:Python中的編碼定義必須位於第1行/第2行嗎?
要定義源代碼的編碼,一個魔法註釋必須被放置到源文件或者作爲該文件中的第一或第二行,比如:
# 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解釋器接受?如果答案解釋了爲什麼它必須位於前兩行,並且指向解釋器代碼的指針非常棒,那將會很棒!
你報的措辭相當清楚的是,編碼必須在第一或第二線。 – BrenBarn
你試過了嗎?你會做什麼來發現這條編碼線是否正在做它打算做的事情? –
我們如何才能知道編碼行是否正在做它打算做的事?在代碼中添加一些'utf8'字符? – alvas