我经常需要临时评论一些代码,但也有类似的情况下,其中评论的一行代码将出现语法错误 Python的NOOP更换
if state == False:
print "Here I'm not good do stuff"
else:
# print "I am good here but stuff might be needed to implement"
有什么事,可能作为一个NOOP保持这个语法正确?
我经常需要临时评论一些代码,但也有类似的情况下,其中评论的一行代码将出现语法错误 Python的NOOP更换
if state == False:
print "Here I'm not good do stuff"
else:
# print "I am good here but stuff might be needed to implement"
有什么事,可能作为一个NOOP保持这个语法正确?
你要找的操作pass
。因此,在您例如,它应该是这样的:
if state == False:
print "Here I'm not good do stuff"
else:
pass
# print "I am good here but stuff might be needed to implement"
你可以阅读更多关于它在这里: http://docs.python.org/py3k/reference/simple_stmts.html#pass
我发现,如果你把代码牛肚引述评论它就像一个空操作,所以你可以把三引述评论,将作为在情况下,代码被删除或#
评论一个空操作。
针对上述案例:
if state == False:
'''this comment act as NOP'''
print "Here I'm not good do stuff"
else:
'''this comment act as NOP and also leaves the
else branch visible for future implementation say a report or something'''
# print "I am good here but stuff might be needed to implement"
在Python 3,...
使一个相当不错的pass
替补:
我将其读为“待完成”,而pass
表示“此页有意留空”。
它实际上只是一个文字,很像None
,True
和False
,但它们都优化了。
您应该使用'pass'(http://docs.python.org/reference/simple_stmts.html#pass)作为noop。这具有短而无附加含义的优点(该字符串可以被程序以不希望的方式解释)。 – Nobody
@没有人你有没有不需要的行为的例子知道该怎么避免? –
裸字符串应该没问题,但是您必须小心,不要将doctests写入这些字符串中(如果不需要)。除此之外,还有不必要的冗长字符串解析,这将被评估和返回只是被忽略(也许这将被优化)。我不能提出比角落案件更不可能的事情,但“通行证”是要走的路,因为它是为了这种情况而制定的。如果您以这种方式使用注释,则可能会破坏注释规则:可以在不更改程序语义的情况下移除它们。 – Nobody