How to write an an inline-comment in python

2019-02-16 05:46发布

Is there a method of ending single line comments in python?

Something like

/*This is my comment*/ some more code here...

2条回答
爷的心禁止访问
2楼-- · 2019-02-16 06:01

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.

查看更多
在下西门庆
3楼-- · 2019-02-16 06:16

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.

查看更多
登录 后发表回答