Python NOOP替换

时间:2012-09-18 10:03:48

标签: python python-2.7 comments 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来保持这种语法正确吗?

3 个答案:

答案 0 :(得分:13)

您正在寻找的操作是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

答案 1 :(得分:6)

在Python 3中,...是一个非常好的pass替代品:

class X:
    ...

def x():
    ...

if x:
    ...

我将其视为“待完成”,而pass表示“此页故意留空”。

它实际上只是一个文字,很像NoneTrueFalse,但它们会优化所有相同的内容。

答案 2 :(得分:4)

我发现如果你把代码放在引用注释'''comment'''的tripe中,它就像一个NOOP,所以你可以放一个三重引用的注释,如果代码被删除或注释{{ {1}}。

对于上述情况:

#