Text of discuss.python.org › t › c-like-multiline-comments › 67371 as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

/* C-like multiline comments */ - Ideas - Discussions on Python.org

/* C-like multiline comments */ Ideas Zezombye (Zezombye) October 9, 2024, 11:54pm 1 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. bschubert (Brian Schubert) October 9, 2024, 11:59pm 2 Previous proposal for /* -style comments: Some other somewhat related comment proposals: 2 Likes Rosuav (Chris Angelico) October 10, 2024, 12:01am 3 So, what you’re saying is: 1.

Triple quoted strings are not comments. 2. Triple quoted strings are not comments. 3. Triple quoted strings are not shown as comments. This should not be a surprise. They are indeed NOT comments. Docstrings are not comments. Triple-quoted strings in other contexts are not comments. It so happens that a string literal on its own does nothing, but that doesn’t make it a comment. Multiline comments have been proposed many times over the years.

What is different about this proposal? Have you answered any of the objections previously raised? Has anything changed since those proposals were made? 2 Likes Zezombye (Zezombye) October 10, 2024, 12:16am 4 To my knowledge the only such proposal was the one that Brian linked above (I’ve searched the forum, the github issues and the PEPs and did not find anything else). That thread brought up issues with indentation (which could indeed maybe be a problem in some circumstances), discussed other syntax (irrelevant to my proposal), and a few objections: “It is too easy to read the unadorned lines of code as actual code” - not an issue with syntax highlighting which is everywhere nowadays.

“The lexer will have increased complexity” - I don’t know anything about that but if the complexity is that big then that should be given as the official reason to not have multiline comments. “It overloads already used operator symbols in completely new and incompatible ways” - I don’t think this is a good argument because that is already the case for existing symbols such as ** dict destructuring which has nothing to do with the power operator, or the | operator for match/case which has nothing to do with binary or.

“It can’t be applied to existing code if that code contains */” - Not an issue to me because this combination of character very rarely occurs anyway. That same argument can be applied to multiline strings as well. “Comments in the middle of a line or logical statement are terrible things to do” - I don’t see why. “It would be a massive amount of churn for all parsers and highlighters” - I can’t contradict that, but I don’t see why adding two tokens to keep track of is a massive amount of churn.

Are there any other topics that already discussed the subject? I was indeed surprised to not see any. Rosuav (Chris Angelico) October 10, 2024, 12:23am 5 Yes, because… they’re not comments. This consideration does not apply to hash comments, which work just fine on lines containing hash characters. The question of whether /* /* */ this part */ is a comment is one that has been debated at length, and there’s no single right answer.

Both answers are somewhat wrong. And yes, I’ve used languages that have made both choices, and have been frustrated by running into their respective issues. But the biggest question is: Why do it? “Because triple quoted strings make bad comments” isn’t a good enough answer. Why do we need slash-star comments? 1 Like MegaIng (Cornelius Krupp) October 10, 2024, 12:25am 6 Mailman 3 Comment syntax similar to /* */ in C? - Python-ideas - python.org Mailman 3 Multi-line comment blocks. - Python-ideas - python.org These are just two I instantly found via quick search in the mailing list , I am sure there are more if you try different terms.

The fact that the discourse proposal is called “ Yet Another Multiline Comment Proposal” should have been an indicator for you that you are missing something. 3 Likes storchaka (Serhiy Storchaka) October 10, 2024, 9:11am 7 So you have syntax highlighting everywhere that supports hypotetical multiline comments, but no editor that supports commenting/uncommenting multiple lines at once? 10 Likes jcampbell05 (James Campbell) October 10, 2024, 1:27pm 8 So if the one of the main issue people are hoping to solve is having multiline comments that maybe don’t need to care as much about indentation as docstrings do since docstrings are part of the code since they are still strings from the POV of python.

The simplest implementation I’ve seen that doesn’t involve implementing c style multiline comments, is in Lua In Lua a single comment is declared using -- If this comment starts with [[ it tells the parser to stop parsing the remaining tokens until it sees another comment begining with ]] From the parsers point of view it’s a simpler thing to implement (Increment a counter for each comment beginning with “[[”, decrement it for every line beginning with “]]” if this counter is bigger than 1 then ignore the following lines of code otherwise process them) Lua --[[ Comment --]] Future Python Multiline comments # [[ Commented out code # ]] Nineteendo (Nice Zombies) October 10, 2024, 4:57pm 9 That wouldn’t be backwards compatible and you can just let your IDE add a # to the start of each line.

1 Like alexrov (Alex Rowan) October 10, 2024, 9:11pm 10 This should not be a surprise. They are indeed NOT comments. It sure does quack like a duck, though. It has always struck me as odd that Python does not have multi-line comments. Users end up utilizing multi-line strings as though they were comments, because there is nothing else to satisfy that use case. That reveals the deficiencies in using them as such. It’s clear that OP understands they are not comments, but that doesn’t change the fact that it’s still how they are used.

I think it’s fair to keep pushing for a feature that would be widely used, even with the failure of previous proposals. Although, I agree that the proposal should be substantively different or add new information or data in support of the request. My main issue with C-style multi-line comment

The rest of this page is on the live site.