- Why doesn’t Python have multiline comments?
- 18 Answers 18
- Python Multiline Comment – How to Comment Out Multiple Lines in Python
- How to Make Single Line Comments in Python
- How to Make Multi-line Comments in Python
- Conclusion
- Multiple line comment in Python [duplicate]
- 2 Answers 2
- Is there a way to put comments in multiline code?
Why doesn’t Python have multiline comments?
But technically speaking these are strings, correct? I’ve googled and read the Python style guide, but I was unable to find a technical answer to why there is no formal implementation of multiline, /* */ type of comments. I have no problem using triple quotes, but I am a little curious as to what led to this design decision.
Just wanted to add, that it fails if what you are trying to comment happens to also have comments/multi-line strings. And that of course, is why we need them.
@S.Lott I think it’s a useful question. In order to understand why Python is good, it’s important to understand the design decisions that were made (and on-going decisions that are still being made). The question isn’t argumentative or combative; it’s curious. No need to be so harsh about curiosity.
@Brody Because strings are processed. Comments are ignored. There are problems with using strings as comments. Just look around 🙂
18 Answers 18
I doubt you’ll get a better answer than, «Guido didn’t feel the need for multi-line comments».
Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! 🙂
one disadvantage of mix multi-line string and block comments is IDE has no idea what you want thus can’t show comment in different style as needed.
It also makes it impossible to comment out code with multi-line strings (and can lead to indentation errors if you’re not careful). Ew!
I have worked in a lot of fields where if your code contains commented out code then your code is rejected and you may even find yourself invited to update your CV. Either remove the code that is not needed, not a problem if the code is under version control, or use if False: before the code that needs to be disabled.
@SteveBarnes agree that big blocks of commented-out code in production are bad. But I don’t understand why if False is better. It accomplishes exactly the same thing, while being less clear (since it isn’t as obvious at a glance that the block of code has been disabled).
Multi-line comments are easily breakable. What if you have the following in a simple calculator program?
operation = '' print("Pick an operation: +-*/") # Get user input here
Try to comment that with a multi-line comment:
/* operation = '' print("Pick an operation: +-*/") # Get user input here */
Oops, your string contains the end comment delimiter.
This is one of the many reasons why we have escape characters, I don’t see that as a good reason to NOT have support for multi-line comments.
I don’t understand your logic — perhaps my comment wasn’t clear enough. If we used \ as an escape character: print(«Pick an operation: +-*\/») «*/» no longer denotes a ending comment block as literally / will be printed. Go ahead and test this in C++. In fact SO’s syntax highlighter will show that is valid. This is not a complex subject, it has existed for years in other languages. I would ask that you update your post to include the use of escape characters to show that you CAN use «*/» in your code.
Multi-line comments aren’t inherently breakable; it’s just that most implementations of them are (including Python’s). The obvious way to do multi-line comments in Python, to my mind, is to just let me start a comment block with #: and use indentation to show when the comment’s ended. It’s clean, consistent, and handles nesting perfectly.
Triple-quoted text should NOT be considered multi-line comments; by convention, they are docstrings. They should describe what your code does and how to use it, but not for things like commenting out blocks of code.
To comment blocks of code, I sometimes use the following pattern:
if False: # A bunch of code
regarding the «if false:» solution, the thing is that in python as it works with tabs, you’d have to tab in all the code under the «if False:». And untab the chunk afterwards. So you’d have to be pretty nifty with your text editor.
Multi-line/triple-quoted strings don’t have to be docstrings, and vice versa. A docstring is «a string literal that occurs as the first statement in a module, function, class, or method definition», whether or not it’s multi-line. Unused (unassigned or not otherwise used in a statement/expression) literals elsewhere in your code, string or otherwise, are discarded at compile time.
This likely goes back to the core concept that there should be one obvious way to do a task. Additional comment styles add unnecessary complications and could decrease readability.
That’s the issue, I believe: Using a string as a comment is not obvious and violates the «one way to do a task» principle, since there are two ways to do comments: strings and # .
But its not significantly different than what you have in C-based languages: /* vs // , so i don’t see how its significantly worse.
// , Consider WHY someone would want a multi-line comment. Good reasons: . I can’t really think of any beyond «I don’t have to type as many of these # doohickeys» and «I need to display this particular comment in a very precise way, and that precise way doesn’t allow for preceding #.» Say someone wants to do an ASCII diagram, or put some reference javascript code to be copied and pasted if a specific problem comes up. The one obvious way to do a task, here, doesn’t cover the edge cases of that task. I agree, though, that additional comment styles are BAD.
«I don’t have to type as many of these # doohickeys». That is precisely why pretty much all languages have block comments (/* ..*/). Believe it or not, but I like to document what my code does: the inputs, the outputs, the algorithms used, the parameters . That is a lot of text that also gets modified. The restriction to only single-line comments is just plain ridiculous. Note that I do NOT advocate the approach for commenting out code — although that is often handy when trying alternate approaches, as long as the well known possible side effects are understood.
The other thing I resent about python is that it is essentially a one-man designed language. Whatever Guido says is the truth . So we have all those odd incompatibilities between language versions. Why ? Because Guido said so .
Python Multiline Comment – How to Comment Out Multiple Lines in Python
Kolade Chris
Commenting is an integral part of every programming language. With comments, you get a better understanding of your own code, make it more readable, and can help team members understand how it works.
Comments are ignored by compilers and interpreters, so they don’t run.
Apart from making your code more readable, comments can also help while you’re debugging – if you have two lines of code, you can comment out one to prevent it from running.
Just like other programming languages, Python supports comments.
The problem is that Python doesn’t have a built-in mechanism for multi-line comments.
So in this article, I won’t just show you how to make single-line comments in Python – I’ll also show you the workaround for making multi-line comments.
How to Make Single Line Comments in Python
To make single-line comments in Python, prepend each line with a hash ( # ).
# print("Hello world") print("Hello campers")
As you can see, the commented line wasn’t printed in the output.
How to Make Multi-line Comments in Python
Unlike other programming languages such as JavaScript, Java, and C++ which use /*. */ for multi-line comments, there’s no built-in mechanism for multi-line comments in Python.
To comment out multiple lines in Python, you can prepend each line with a hash ( # ).
# print("Hello world") # print("Hello universe") # print("Hello everyone") print("Hello campers")
With this approach, you’re technically making multiple single-line comments.
The real workaround for making multi-line comments in Python is by using docstrings.
If you use a docstring to comment out multiple line of code in Python, that block of code will be ignored, and only the lines outside the docstring will run.
""" This is a multi-line comment with docstrings print("Hello world") print("Hello universe") print("Hello everyone") """ print("Hello campers")
NB: One thing to note is that while using doctsrings for commenting, indentation still matters. If you use 4 spaces (or a tab) for indentation, you will get an indentation error.
For example, this will work:
def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4)) # Output: 9
def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4))
So your IDE will throw the error » IndentationError: expected an indented block «.
Conclusion
Since there’s no built-in support for multi-line comments in Python, this article demonstrates how you can use docstrings as a workaround.
Still, you should generally stick to using regular Python comments using a hash ( # ), even if you have to use it for multiple lines. This is because docstrings are meant for documentation, and not for commenting out code.
If you found this article helpful, consider sharing it with your friends and family.
Multiple line comment in Python [duplicate]
Is there a way to give multiple line comments in Python? Like it is in case of C/C++ : /*comment*/ . Or does it have to be marked «#» in front of every line?
It’s worth pointing out (despite the tag), that in C and C++ a multi-line comment is given by opening /* and closing */ , not /- .
2 Answers 2
''' This is a multiline comment. I can type here whatever I want. '''
Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode — just like #-prepended comments. In effect, it acts exactly like a comment.
On the other hand, if you say this behavior must be documented in the official docs to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.
In any case your editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to an editor that does.
Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.
Not only should the editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.
Is there a way to put comments in multiline code?
Actually, according to PEP8 parentheses are preferred over slashes, when breaking something into multiple lines:
The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
In your case it also allows to put comments.
Here is a proof, that it works: http://ideone.com/FlccUJ
@Yotam: Actually, see the link attached to «according to PEP8» text in my answer. Also, the quote from my answer goes directly from what you have pasted here.
What should we do to split over the dots in this case ? obj.method1(args1).method2(args2).method3(args3)
Not sure of an in-line way, but you can do a=obj.method1(args) # comm1 & b=obj.method2(args2) # comm2 etc.
@lago-lito: In such case I would split it using parentheses, too. If you want to comment each segment, then obviously you can still do this as per PEP8: obj.method1( \n # Important method 1 \n args1 \n ).method2( \n # Importand other method \n args2 \n ).method3( \n # Method that needs to be called last \n args3 \n ) . Maybe it looks too expressive, but hey — it is you who wants to comment every method in the chain 😉
Not sure what you are trying to do is supported by python. Read PEP8 section about inline comments. Putting comments in the middle of line continuations is «ugly» and probably confusing.
Python way is with # on every line if you want to comments something or for inline comments everything after # is ignored.
If you really want to comment a multiline statement that is really necessary put it before or after it.
a, b, c, d = range(1, 5) # a is . b is . # c is . d is . result = (a, b, c, d)
definately don’t want to get into an argument about style, but just because you can do something doesn’t mean that it is clear. Inline comments are great for clarifyling short lines of code that just need a short pointer.