Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Encoding Comment Format

Originally, I've learned to specify the source code encoding in Python 2.7 this way:

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

Now I just noticed, that PEP263 also allows this:

# coding=utf-8

Is there any differences between these? What about editor compatiblity, cross-platform etc.?

What about Python 3? Is this comment still needed for python 3 or is any code in python 3 expected to be utf-8 by default?

like image 815
Micha Avatar asked Jan 01 '23 21:01

Micha


1 Answers

Take a look at PEP3120 which changed the default encoding of python source code to be UTF-8

For python 3.x one therefore finds in the docs:

If a comment in the first or second line of the Python script matches the regular expression coding[=:]\s*([-\w.]+), this comment is processed as an encoding declaration [...] The recommended forms of an encoding expression are:

# -*- coding: <encoding-name> -*-

which is recognized also by GNU Emacs, and

# vim:fileencoding=<encoding-name>

which is recognized by Bram Moolenaar’s VIM. If no encoding declaration is found, the default encoding is UTF-8

The take home message is therefore:

  1. python 3.x does not neccessarily need to have utf-8 specified, since it is the default
  2. The way the coding line is written is to some degree personal choice (only a recommendation in the docs), it only has to match the regex.
like image 51
FlyingTeller Avatar answered Jan 05 '23 17:01

FlyingTeller