How to write an inline-comment in Python

How to write an inline-comment in Python

No, there are no inline comments in Python.

From the documentation:

A comment starts with a hash character (#) that is not part of a
string literal, and ends at the end of the physical line. A comment
signifies the end of the logical line unless the implicit line joining
rules are invoked. Comments are ignored by the syntax; they are not

Whitespace in Python is too important to allow any other kind of comment besides the # comment that goes to the end of the line. Take this code:

x = 1
for i in range(10):
             x = x + 1
/* Print. */ print x

Because indentation determines scope, the parser has no good way of knowing the control flow. It cant reasonably eliminate the comment and then execute the code after it. (It also makes the code less readable for humans.) So no inline comments.

How to write an inline-comment in Python

No, there are no inline-block comments in Python.
But you can place your comment (inline) on the right.
Thats allows you to use syntax and comments on the same line.
Anyway, making comments to the left of your code turn reading difficult, so…


x = 1 # My variable

Leave a Reply

Your email address will not be published. Required fields are marked *