Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is '# -*- coding: utf-8 -*-' also a comment in Python?

As we use # for inserting comments in Python, then how does Python treat:

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

differently?

like image 853
ngShravil.py Avatar asked Jan 16 '17 16:01

ngShravil.py


People also ask

What is the meaning of is?

1 : equal : homogeneous : uniform isentropic. 2 : isomeric isocyanate.

Is a is a verb?

Is is a verb or a noun? Is it a preposition? In this post, we have learned that the word is a verb and functions solely as a verb to describe a state of being or existence. Is is a verb.

Is in use a word?

Definition of in use : being used All of the computers are currently in use.

How do you write as is?

As-is definition Alternative spelling of as is. (idiomatic, of an object) As it is; its present state or condition, especially as a contractual condition of sale. I bought the car as is, so the seller was within his legal rights to refuse to repair it when it broke down after two days.


2 Answers

Yes, it is also a comment. And the contents of that comment carry special meaning if located at the top of the file, in the first two lines.

From the Encoding declarations documentation:

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 encoding declaration must appear on a line of its own. If it is the second line, the first line must also be a comment-only line.

Note that it doesn't matter what codec should be used to read the file, as far as comments are concerned. Python would normally ignore everything after the # token, and in all accepted source code codecs the #, encoding declaration and line separator characters are encoded exactly the same as they are all supersets of ASCII. So all the parser has to do is read one line, scan for the special text in the comment, read another if needed, scan for the comment, then configure the parser to read data according to the given codec.

Given that the comment is required to be either the first or second in the file (and if it is the second line, the first line must be a comment too), this is entirely safe, as the configured codec can only make a difference to non-comment lines anyway.

like image 90
Martijn Pieters Avatar answered Sep 19 '22 08:09

Martijn Pieters


See encoding declarations in the Python Reference Manual:

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.

(Emphasis mine)

So yes, it is a comment, a special one. It is special in that the parser will try and act on it and not ignore it as it does for comments not in the first or second line. Take, for example, an unregistered encoding declaration in a sample file decl.py:

# # -*- coding: unknown-encoding -*- print("foo") 

If you try and run this, Python will try and process it, fail and complain:

python decl.py    File "decl.py", line 1 SyntaxError: encoding problem: unknown-encoding 
like image 20
Dimitris Fasarakis Hilliard Avatar answered Sep 21 '22 08:09

Dimitris Fasarakis Hilliard