4

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?

2 Answers 2

6

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.
Sign up to request clarification or add additional context in comments.

Comments

3

Since Python 3 the default encoding is utf-8. You can still change the encoding using the special-formatted comment # -*- coding: <encoding name> -*-.

The docs recommend to use this coding expression as it is recognized also by GNU Emacs.

As python checks whether the first two lines are matching the regex coding[=:]\s*([-\w.]+),
# coding=utf-8 works also to ensure utf-8 encoding but it is not recognized by GNU Emacs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.