- publishing free software manuals
Python Language Reference Manual
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 120 pages
ISBN 0954161785
RRP £12.95 ($19.95)

Sales of this book support the Python Software Foundation! Get a printed copy>>>

2.1.4 Encoding declarations

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 first group of this expression names the encoding of the source code file. The recommended forms of this 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. In addition, if the first bytes of the file are the UTF-8 byte-order mark ('\xef\xbb\xbf'), the declared file encoding is UTF-8 (this is supported, among others, by Microsoft's notepad).

If an encoding is declared, the encoding name must be recognized by Python. The encoding is used for all lexical analysis, in particular to find the end of a string, and to interpret the contents of Unicode literals. String literals are converted to Unicode for syntactical analysis, then converted back to their original encoding before interpretation starts. The encoding declaration must appear on a line of its own.

ISBN 0954161785Python Language Reference ManualSee the print edition