thnkwthprtls
thnkwthprtls

Reputation: 3487

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

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.

Upvotes: 2

Views: 225

Answers (6)

Jack Aidley
Jack Aidley

Reputation: 20107

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.

Upvotes: 2

tobias_k
tobias_k

Reputation: 82899

Python does not have inline or block comments like this. You can add a string (or any other expression), as suggested by others, but you will have to make sure to (consistently) replace all of those placeholders, which is extremely error prone

If it's only the value of the variable that is missing or unclear, and not the variable itself, how about this:

variable_to_be_defined = None # TODO define me!
some_other_variable = variable_to_be_defined + an_existing_variable

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28370

Why not use something like:

name = "Use this for something later"
:
:
name = 27

Upvotes: 0

Germano
Germano

Reputation: 2482

You can't: According to the documentation, comments in Python start with the hash character (#) and extend to the end of the physical line. See An Informal Introduction to Python.

Upvotes: 0

Brian
Brian

Reputation: 3131

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)

Upvotes: 6

recursive
recursive

Reputation: 86064

Inline comments do not exist in python.

The closest that I know of is the use of strings:

int some_variable = "some_variable_that_doesnt_exist_yet +" and an_existing_variable;

But that is terrible, and you should never do that.

Upvotes: 1

Related Questions