The Python Oracle

Where does this come from: -*- coding: utf-8 -*-

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Question
00:49 Accepted answer (Score 106)
01:20 Answer 2 (Score 59)
02:06 Answer 3 (Score 10)
02:28 Answer 4 (Score 4)
02:48 Thank you

--

Full question
https://stackoverflow.com/questions/4872...

Accepted answer links:
[PEP 0263 - Defining Python Source Code Encodings]: http://www.python.org/dev/peps/pep-0263/
[Python Language Reference, 2.1.4 Encoding declarations]: http://www.network-theory.co.uk/docs/pyl...

Answer 2 links:
[is already UTF-8]: https://docs.python.org/3/howto/unicode....
[some variation]: https://docs.python.org/3/library/codecs...
[Should I use encoding declaration in Python 3?]: https://stackoverflow.com/q/14083111/306...
[pyupgrade]: https://pypi.org/project/pyupgrade/

Answer 3 links:
[section in Emacs manual]: https://www.gnu.org/software/libtool/man...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #file #text #encoding #emacs

#avk47



ACCEPTED ANSWER

Score 107


This way of specifying the encoding of a Python file comes from PEP 0263 - Defining Python Source Code Encodings.

It is also recognized by GNU Emacs (see Python Language Reference, 2.1.4 Encoding declarations), though I don't know if it was the first program to use that syntax.




ANSWER 2

Score 64


# -*- coding: utf-8 -*- is a Python 2 thing.

In Python 3.0+ the default encoding of source files is already UTF-8 so you can safely delete that line, because unless it says something other than some variation of "utf-8", it has no effect. See Should I use encoding declaration in Python 3?


pyupgrade is a tool you can run on your code to remove those comments and other useless leftovers from Python 2, like having all your classes inherit from object.




ANSWER 3

Score 11


This is so called file local variables, that are understood by Emacs and set correspondingly. See corresponding section in Emacs manual - you can define them either in header or in footer of file




ANSWER 4

Score 4


In PyCharm, I'd leave it out. It turns off the UTF-8 indicator at the bottom with a warning that the encoding is hard-coded. Don't think you need the PyCharm comment mentioned above.