Using multiline strings as mutliline comments, as is commonly done, has several disadvantages:
- Multiline strings cannot be used as comments in several places.
Middle of array (or function call, or tuple…):
some_array = [
1,
2,
"""3,
4,"""
5,
6
]
-> SyntaxError (no comma after string)
some_array = [1,2,3,4,5,6] #it is impossible to comment out 3,4 without unnecessarily splitting the array into new lines
Between if/elif/else (elif raises SyntaxError when preceded by a string statement · Issue #108272 · python/cpython · GitHub)
if 1:
foo = 2
"""
This is very important.
Keep it.
"""
else:
foo = 3
-> SyntaxError (the string reset the indentation)
- Raw strings must be used when the comment contains an escape.
See: Python fails to parse triple quoted (commented out) code · Issue #75436 · python/cpython · GitHub, Unicode errors occur inside of multi-line comments · Issue #73471 · python/cpython · GitHub
'''
log = list(r'..\Unknown\*.txt')
'''
-> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes: truncated \UXXXXXXXX escape
It sometimes also gives out a SyntaxWarning for unrecognized escapes.
- Multiline strings are not rendered as comments by syntax highlighters, which makes it more difficult to parse the code and skim over the parts that are comments.
For all intents and purposes, Python already has multiline comments (they are even officially parsed as docstrings in PEP 8 – Style Guide for Python Code | peps.python.org). It’s just that currently, those multiline comments sometimes fail in various ways and sometimes can’t even be used.
Having specific syntax for multiline comments would allow for them to be used anywhere and not be a hack.
Additional requests:
- Have
/**
denote the start of a docstring, which are used the same as the current docstrings and are put in the__doc__
attribute. - Have multiline comments be able to be nested. This is unlike other C-like languages but more easily allows to comment out code that already has multiline comments in it.