0

My question is a simple one. What is the criteria to classify if something is a comment or isn't a comment. I am asking this as a general question. But I will be using python as a way to express my question.

From the accepted and highly upvoted answer How do I create multiline comments in Python?, I found that triple quotes can be used as a comment. But if I enter a character like \x, I get an error. At that time, definition of comment for me was a text inserted in the code for future reference and making code more understandable and this text is always ignored by the compiler/interpreter. So, I believed that the answer was wrong. Then for some reason I went to Wikipedia to read the definition of comment. Quoting from https://en.m.wikipedia.org

a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

In the Wikipedia page, it is mentioned generally ignored and not always ignored. So triple quotes can be considered as comments. But if a multiline string can be considered as a multiline comment, why can't a single line string be considered as a single line comment?

To summarise, my question is simply that what is the criteria using which we can decide if a statement is a comment or not?

Sorry for my poor English.

user12137152
  • 732
  • 1
  • 5
  • 14
  • 1
    Simply said every allowed `#` outside of a string or a comment starts a comment and it spans to the end of the line. Quotation marks don't start comments but strings, even though strings can be (ab)used as comments. – Klaus D. Oct 12 '22 at 18:17

4 Answers4

0

The triple-quoted string you're referring to are actually not comments but strings. They are sometimes used in lieu of comments because they can be multi-line, which makes them appropriate for docstrings, but they are also used as multi-line string objects - meaning you can assign them to a variable and treat them like a string. Actual comments # can't be assigned to a variable, which is the difference.

Hunter Boyd
  • 165
  • 10
0

As you've noticed, a triple quoted string in python is still a string; i.e. it can be assigned to a variable, manipulated, etc. the same way a string can be.

This isn't the most useful feature ever since it includes tabs, etc., but it is a feature of python that can be helpful if you need to hard code a paragraph of text and don't want to split it up into multiple strings.

For example the following code will run without errors

foo = """this is a long block of text
that gets converted into a single string
with new line characters and all"""

print(foo)

If you want to use docstrings as variables, inspect.cleandoc() will clean up white space and adjust indents in a string the same way python cleans up docstrings

What happens when you use triple quotes is that you declare the value of a base type and assign it to nothing, which then leads to it being dropped when the line of code is run.

This is functionally no different from creating an instance of a class and not assigning it to a variable; it's a bit silly to do, but it does run through the relevant __init__() calls. It can be done with any type not just docstrings.

For example the following is valid python code. It does nothing, but it will run without errors and can be loosely regarded as a comment in the same way that triple quoted strings are.

1
2
3
4
5
"this is also a comment if you think about it; it does nothing!"

Python does not have real multi line comments (a mechanism to completely ignore text in source files across multiple lines with a starting and terminating character / character sequence). It's generally a good idea to stick to PEP-8 guidelines and use # once per line to avoid unexpected issues.

imbuedHope
  • 508
  • 3
  • 14
0

A triple quoted string isn't a comment; it's a multiLine String which could become a Docstring:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.

It's indeed true that there should be always only one way to do it..In Python we have 2 ways to do it.

Actual recomended official way to do it

#comment 1
#comment 2

Other ways to do it is multiLine comment

'''

comment1
comment2

'''

In second case it's not a comment. It is a multiLine String

Python written in C ...Compilers detect comments in parsing stage The orginal way something compiler detects if it is comment or not

if(StartsWith("#some code line", "#")) { //It will treat as comment
   //Strip that line
}else {
  // do something else
}

So in your case why uisng \x inside multiline string causing error

The '\x' escape sequence signifies a Unicode character in the string, it is being interpreted as the hex code. Refrence

-1

A comment is essentially just one sentence or more that can be used to indicate what a piece of code means, for example

#this function prints "Hello world!"
 def printer()
   print("Hello world!")

To Comment multiple lines, add triple quotations (""") at the top and bottom of your statement

"""
This is a comment
and can wrap to multiple
lines.
"""