Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create inline comments (without using backslash to continue on another line)?

Tags:

python

Sorry in advance if this is something really easy, I'm very new to Python. This seems like it should be something very simple, but I am having a frustratingly hard time finding an answer online.

I'm writing a Python script in a preliminary, pseudo sort of fashion at the moment, and don't have all variable defined yet. I want to be able to have comments in the middle of a line to symbolize where the variable will go, but without commenting out the entire rest of the line to the right.

To visualize what I mean, this is how what I want is done in C/C++:

int some_variable = /*some_variable_that_doesnt_exist_yet*/ + an_existing_variable;

Basically I need to be able to comment the middle of a line without commenting the left or right sides of said comment. Is there any way to do this?

I know there's a way to do it like this (or something similar to this):

some_variable = #some_variable_that_doesnt_exist_yet
                \+ an_existing_variable

...but I'd rather not do it that way if possible, just for ease of reading.

like image 236
thnkwthprtls Avatar asked Dec 11 '25 21:12

thnkwthprtls


2 Answers

Unfortunately no. But you can always break things into multiple lines and comment in between. Parentheses come in handy for that.

my_var = (#some_variable +
         some_other_var)
like image 65
Brian Avatar answered Dec 13 '25 11:12

Brian


As with any language switch you will need to learn new habits that fit the features of the language. Python does not have the feature you desire, you could use some horrendous hack to force something that looks a bit similar in but I rather suggest you don't.

Some options are: document the TODO on a neighbouring line, perhaps using docstrings; don't sweat it and figure you'll add it later when your tests start requiring it; or use the fact that variables are lightweight and just create them with dummy values that leave the final calculation unchanged.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!