How to write an inline-comment in Python

本小妞迷上赌 提交于 2019-12-18 18:36:10

问题


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

Something like

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

回答1:


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.




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/24872745/how-to-write-an-inline-comment-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!