经常需要临时注释一些代码,但有些情况下注释一行代码会导致语法错误,如下所示:
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,以保持这个语法的正确性?
经常需要临时注释一些代码,但有些情况下注释一行代码会导致语法错误,如下所示:
if state == False:
print "Here I'm not good do stuff"
else:
# print "I am good here but stuff might be needed to implement"
您要查找的操作是 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...
可以很好地替代pass
:class X:
...
def x():
...
if x:
...
我理解它的含义是“待完成”,而pass
则表示“此页面有意留空”。
实际上,它只是一个字面量,就像None
、True
和False
一样,但是它们都可以被优化掉。
'''注释'''
中,它就像一个NOOP,因此你可以放一个三引号注释,以防代码被删除或使用#
注释掉。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"
pass
(http://docs.python.org/reference/simple_stmts.html#pass)作为空操作。这样做的优点是代码短小而且没有额外的含义(字符串有可能被程序误解释)。 - Nobody moving away from SE1+1
,或者说 - 关键是pass
已经意味着“什么也不做”,以便其他人阅读代码时能够理解它什么也不做。 - Katrieldef
或class
之后或模块的开头,因此不是文档字符串,doctest 就会忽略这些字符串。 - unutbu