Most commonly multi-line strings are used for this:
print('Hello, world!')
"""The previous statement
prints 'Hello, world!'."""
But pylint (rightfully) complaints about this: test.py:2:0: W0105: String statement has no effect (pointless-string-statement)
Additionally, they can’t be used midline or where docstrings are expected:
"""Hello, I'm
a docstring."""
a = '1' + '2' + '3' + """This will be included in the string.""" \
'4' + '5'
for i in range("""stop/"""5): # SyntaxError: invalid syntax. Perhaps you forgot a comma?
print(i)
What if we had raw-string literals that were ignored by the compiler by prefixing the string with 'c'?
c"""Hello, I am a
multiline comment."""
a = '1' + '2' + '3' + c"This will not be included in the string." \
'4' + '5'
for i in range(c"stop/"5):
print(i)
Parsing these shouldn’t be too difficult, and users can easily convert their multi-line strings to c-strings by prefixing them with a 'c' (or replace the 'r' prefix).
There are no multiline comments in Python (no matter what anyone says). If you want a comment that spans multiple lines, use one # on each line. If that’s annoying to do, then upgrade your editor.
This has been brought up many times by the way, so it’s probably worth searching old posts.
Incidentally, you probably shouldn’t use line continuations in your example instead of parentheses, but this is a matter of style.
Thus, your example in conventional Python is:
# Hello, I am a
# multiline comment.
a = ('1' + '2' + '3' # This will not be included in the string.
+ '4' + '5')
for i in range(5): # 5 is the stop parameter.
print(i)
Either they’re strings or they’re comments. If you want them to behave like /* ... */ comments in C-like languages, don’t make them look like strings.
So, they’re nothing whatsoever to do with string literals. Why abuse string literal notation for them? Why not just propose the introduction of /* */ comments? At least people (and tools) will know what to expect.
Correction, this is not related to multi-line comments.
Fair enough, but I would say the midline comment is clearer.
In theory it would be easier to parse, as you can just reuse the string parsing logic.
You can also easily convert strings used as comments to actual comments (no escaping is required).
Explaining this to Python users would be simple: they’re parsed the same way as r-strings, but ignored by the compiler. Just add a 'c' prefix to the strings you used as block comments.
It’s harder to correctly parse string literals than to parse comments. There are three popular types of comment syntax, all very easy to parse:
A token meaning “comment till end of line”. Python’s hash comments are of this form, as are SQL’s double-hyphens. Once a comment has started, the only thing you need to look for is a line ending. (There may be an exception for an escaped end-of-line but that’s all.)
Non-nestable comments. Once you find the start-comment token (eg /*), the only thing you need to search for is the end-comment token (eg */). Extremely simple to parse. Does not care about line endings or anything. Has the consequence that a comment inside a block of code to be commented-out will break it.
Nestable comments. From the start-comment token, you need to search for two things: another start-comment sequence, or the end-comment sequence. (Commonly /* and */ again.) This has the consequence that a start-comment sequence inside a string literal will break the comment (thus leading to stylistic choices like concatenating two string literals just to avoid having that happen).
All three are far simpler than the complexities of string literals, which recognize a variety of escaping rules. For example, this is a valid string literal:
x = "start \\\
and end"
print(x)
With just two backslashes, it wouldn’t be valid.
But reusing string literal parsing would lead to unnecessary confusion. People do not expect string literal behaviour from comments or vice versa. You’re gaining nothing but confusion by this.
I honestly don’t think there’ll be a lot of support for a /* */ comment proposal, but it has a better chance than something based on string literals IMO.
I won’t deny that, but I was talking about the Python compiler and not about other tools.
I’ll note that highlighting unused string literals as comments is even harder than detecting c-strings, because they don’t have a special prefix.
There are 2 solutions if this is a problem:
Don’t allow single line c-strings
Don’t allow line continuation for c-strings, but at that point you could just support /**/.