Who originally invented this type of syntax: -*- coding: utf-8 -*-


Python recognizes the following as instruction which defines file’s encoding:

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

I definitely saw this kind of instructions before (-*- var: value -*-), so I assume Python did not invent them and is not the only one that uses such instructions.

Where does this syntax come from? Is there full specification, e.g. can the value include spaces, special symbols, newlines, even -*- itself?

What other software recognizes such metadata syntax?

My program will be writing plain text files and I’d like to include some metadata in them using this format.

Asked By: hamstergene



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.

Answered By: Andrea Spadaccini

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

Answered By: Alex Ott

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.

Answered By: cwp393

# -*- 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.

Answered By: Boris Verkhovskiy
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.