I have looked at other questions about this and the general consensus seems to be that ##
is used for commenting out code. However, in the Interactive Editor for Python, prefixing a line with ##
appears to make a sort of section header for the code. Why is this?
1 Answer
In Python's syntax, a hash indicates the start of a comment, and all subsequent characters on the line are ignored. To quote the language reference:
A comment starts with a hash character (
#
) that is not part of a string literal, and ends at the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Comments are ignored by the syntax; they are not tokens.
The second hash therefore has no syntactic meaning in Python itself.
However, assuming you mean this specific "Interactive Editor for Python", one of its features is:
Matlab-style cell notation to mark code sections (by starting a line with
##
).
Because the comments are not meaningful syntax, tools are free to interpret and use them how they like. Another example of this is pylint
, which allows specially-formatted comments (# pylint: disable=message-name
) to deactivate specific messages.