Should I use encoding declaration in Python 3?

后端 未结 2 884
轻奢々
轻奢々 2020-11-28 06:25

Python 3 uses UTF-8 encoding for source-code files by default. Should I still use the encoding declaration at the beginning of every source file? Like # -*- coding: ut

2条回答
  •  一整个雨季
    2020-11-28 07:06

    No, if:

    • entire project use only the UTF-8, which is a default.
    • and you're sure your IDE tool doesn't need that encoding declaration in each file.

    Yes, if

    • your project relies on different encoding
    • or relies on many encodings.

    For multi-encodings projects:

    If some files are encoded in the non-utf-8, then even for these encoded in UTF-8 you should add encoding declaration too, because the golden rule is Explicit is better than implicit.

    Reference:

    • PyCharm doesn't need that declaration:

    configuring encoding for specific file in pycharm

    • vim doesn't need that declaration, but:
    # vim: set fileencoding= :
    

提交回复
热议问题