6

多くの場合、一部のコードに一時的にコメントする必要がありますが、コードの 1 行にコメントを付けると構文エラーが発生する次のような状況があります。

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 として機能するものはありますか?

4

3 に答える 3

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

于 2012-09-18T10:24:49.100 に答える
6

Python 3 では...、かなり適切なpass代用になります。

class X:
    ...

def x():
    ...

if x:
    ...

私はそれを「やるべき」と読みますが、pass「このページは意図的に空白のままにした」という意味です。

Noneこれは実際には,Trueとによく似た単なるリテラルですFalseが、それらはすべて同じように最適化されます。

于 2014-09-08T11:23:49.610 に答える
4

コードを'''comment'''三重引用符で囲んだコメントに入れると、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" 
于 2012-09-18T10:03:47.597 に答える