How to write an inline-comment in Python
Solution 1
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 can't reasonably eliminate the comment and then execute the code after it. (It also makes the code less readable for humans.) So no inline comments.
Solution 2
No, there are no inline-block comments in Python. But you can place your comment (inline) on the right. That's allows you to use syntax and comments on the same line. Anyway, making comments to the left of your code turn reading difficult, so...
Ex:
x = 1 # My variable
Solution 3
This is pretty hideous, but you can take any text convert it into a string and then take then length of that string then multiply by zero, or turn it into any kind of invalid code. example
history = model.fit_generator(train_generator,steps_per_epoch=8,epochs=15+0*len(", validation_data=validation_generator"), validation_steps=8,verbose=2)
Solution 4
If you're doing something like a sed
operation on code and really need to insert plain text without interfering with the rest of the line, you can try something like:
("This is my comment", some more code here...)[1]
Eg.,
my_variable = obsolete_thing + 100
could be transformed with sed -e 's/obsolete_thing/("replacement for &", 1345)[1]/'
giving:
my_variable = ("replacement for obsolete_thing", 1234)[1] + 100
Solution 5
You can insert inline comment. Like this
x=1; """ Comment """; x+=1; print(x);
And my python version is "3.6.9"
Cease
Updated on July 05, 2022Comments
-
Cease almost 2 years
Is there a method of ending single line comments in Python?
Something like
/* This is my comment */ some more code here...
-
mgilson almost 10 yearsI'm not sure there is any need to blame this on the parser's sensitivity to whitespace. You could just say that the line starts where the comment starts if you wanted... I think it's more the philosophy that the middle of a line is no place for a comment. :-)
-
ABMagil almost 10 yearsThe parser isn't the only thing that reads the code... Personally, I would rather read python where lines start where the characters start. It's not a huge deal, but it's the little things that make python easy and fun.
-
TheSoundDefense almost 10 years@ABMagil that is true... a human parser is probably more error-prone than the actual parser :)
-
TheJP almost 7 yearsI agree that comments inline before or after code are "ugly". But it can be very useful to comment out a specific part (inline) while debugging and I'd like to do the following (new lines after each '\'):
data_frame \ # .coalesce(1) \ .write \ .option('header', 'true') \ # Comment about csv file format \ .csv(file_name)
-
Braden Best almost 6 yearsBut what about line continuations? The sequence,
\#
, raises a SyntaxError because apparently they decided to make whitespace a part of the line continuation token. I can't even move it to the next line because the comment terminates the line continuation. Why? This behavior is nonsensical. The sequence\^J#
should just result in an implicit line continuation continuation after the comment terminates, or\#
should be allowed, or there needs to be a dumb inline comment that interprets[comment] stuff
as just ` stuff`, all consequences included. -
Hamza almost 3 yearsHow this relates to the question in any way?
-
Roland Pihlakas over 2 years@Hamza The string part can be considered as an inline comment.
-
Lenormju about 2 yearsNot very "pythonic" but it does what is asked. Works in any Python version.