6

Often I need to temporary comment some code, but there are situations like the following where commenting a single line of code will get a syntax error

if state == False:
    print "Here I'm not good do stuff"
else:
#    print "I am good here but stuff might be needed to implement"

Is there something that might act as an NOOP to keep this syntax correct?

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179

3 Answers3

14

The operation you're looking for is pass. So in your example it would look like this:

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"

You can read more about it here: http://docs.python.org/py3k/reference/simple_stmts.html#pass

while
  • 3,602
  • 4
  • 33
  • 42
8

In Python 3, ... makes a pretty good pass substitute:

class X:
    ...

def x():
    ...

if x:
    ...

I read it as "to be done", whereas pass means "this page intentionally left blank".

It's actually just a literal, much like None, True and False, but they optimize out all the same.

Veedrac
  • 58,273
  • 15
  • 112
  • 169
4

I discovered that if you put the code in tripe quoted comment '''comment''' it acts like a NOOP, so you can put a triple quoted commentary that will act as a NOOP in case the code gets deleted or commented with #.

For the above case:

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" 
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • 7
    You should use `pass` (http://docs.python.org/reference/simple_stmts.html#pass) as the noop. This has the advantage of being short and having no additional meaning (the string can be interpreted in unwanted ways by the program). – Nobody moving away from SE Sep 18 '12 at 10:05
  • @Nobody Do you have an example of unwanted behavior to know what to avoid? – Eduard Florinescu Sep 18 '12 at 10:10
  • Bare strings should be okay but you must be careful to not write doctests into these strings (if they are not wanted). Apart from that there is the unnecessary parsing of lengthy strings, that will be evaluated and returned just to be ignored (maybe this will be optimized away). I cannot come up with more than corner cases that are very unlikely but nonetheless `pass` is the way to go because it was made for this situation. If you use comments this way you break the rule of comments: that they can be removed without changing the programs semantics. – Nobody moving away from SE Sep 18 '12 at 10:17
  • 2
    Agreed. You could also do `1+1`, say -- the point is that `pass` _already means_ "do nothing", so that someone else reading the code understands that it does nothing. – Katriel Sep 18 '12 at 10:32
  • 2
    Multiline strings can be used as comments and [they generate no code](https://twitter.com/gvanrossum/status/112670605505077248). Also, as long as these strings are not placed after `def` or `class` or at the beginning of a module, and thus are not docstrings, [doctest ignores these strings](http://ideone.com/kwcET). – unutbu Sep 18 '12 at 12:12