19

Is there any difference between using

#coding=utf8

and

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

What about

# encoding: utf-8
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
orome
  • 45,163
  • 57
  • 202
  • 418

1 Answers1

26

There is no difference; Python recognizes all 3. It looks for the pattern:

coding[:=]\s*([-\w.]+)

on the first two lines of the file (which also must start with a #).

That's the literal text 'coding', followed by either a colon or an equals sign, followed by optional whitespace. Any word, dash or dot characters following that pattern are read as the codec.

The -*- is an Emacs-specific syntax; letting the text editor know what encoding to use. It makes the comment useful to two tools. VIM supports similar syntax.

See PEP 263: Defining Python Source Code Encodings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343