Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an inline-comment in Python

People also ask

How do you write inline comments?

All characters immediately following the // syntax until the end of the line will be ignored by JavaScript. Block comments, sometimes referred to as mutli-line comments, are written with opening tags ( /* ) and closing tags ( */ ).

How do you comment a whole section in Python?

Unlike other programming languages Python doesn't support multi-line comment blocks out of the box. The recommended way to comment out multiple lines of code in Python is to use consecutive # single-line comments. This is the only way to get “true” source code comments that are removed by the Python parser.

How do you comment out multiple lines in Python?

To comment out multiple lines in Python, you can prepend each line with a hash ( # ).


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 tokens.


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.


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


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)