When to use utf8 as a header in py files

后端 未结 5 1575
误落风尘
误落风尘 2021-02-05 05:32

Some source files, from downloaded code, have the following header

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

I have an idea what utf-8 encoding is but why would

5条回答
  •  爱一瞬间的悲伤
    2021-02-05 05:46

    A more direct answer:

    In Python 3+: you don't need to declare. UTF-8 is the default. Make sure the file is encoded in UTF-8. Some Windows editors don't have it by default. It won't hurt to declare it, and some editors may use it.

    In Python 2: always. The default is OS dependent.

    And remember: this is just about your source code files. Now in the 3rd millennium the string type does not exist anymore. You must take care of the type text, that is a sequence of bytes and an encoding. You'll still have to define your encoding in all input and output operation. These operations will still be dependent on your environment, so it's still better to follow the rule: Explicit is better than implicit.

提交回复
热议问题