如何在 Python 中编写内联注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24872745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write an inline-comment in Python
提问by Cease
Is there a method of ending single line comments in Python?
有没有一种在 Python 中结束单行注释的方法?
Something like
就像是
/* This is my comment */ some more code here...
采纳答案by Cease
No, there are no inline comments in Python.
不,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.
注释以
#
不属于字符串文字的散列字符 ( )开头,并在物理行的末尾结束。除非调用隐式行连接规则,否则注释表示逻辑行的结束。注释被语法忽略;它们不是令牌。
回答by TheSoundDefense
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:
Python 中的空格太重要了,除了#
行尾的注释之外,不允许任何其他类型的注释。拿这个代码:
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.
因为缩进决定了作用域,解析器没有很好的方法来了解控制流。它不能合理地消除注释,然后执行它之后的代码。(这也降低了代码对人类的可读性。)所以没有内联注释。
回答by Emnolope72
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)