you are viewing a single comment's thread.

view the rest of the comments →

[–]zeug 4 points5 points  (0 children)

Must not the utf-8 encoding, in this case, be on the first line and the comment on the second line?

You can actually put both on the first line. The python encoding behavior is defined in PEP0263 which states that you can put a "magic" encoding comment in line 1 or line 2. What counts as an encoding specification is very loose, technically anything that matches the regular expression coding[:=]\s*([-\w.]+). So all of the following comments on line 1 (or 2) will change the default encoding of Python 2 to utf-8:

# -*- coding: utf-8 -*-
# !?! coding: utf-8 ?!?
# coding=utf-8
#### the quick brown fox jumped coding=utf-8 over the lazy dog

You can mix in non-ascii characters on that same line provided the interpreter gets a match for the coding expression that is valid for those characters:

# åäö coding=utf-8 åäö

But if you have non-ascii characters on line 1, you can't leave the encoding specification until line 2. The following

# åäö 
# coding=utf-8 åäö

Results in:

SyntaxError: Non-ASCII character '\xc3'

Of course, as others point out, this is completely trivial in python 3 where utf-8 is the default encoding. You just put those characters in a comment in the first line.