comment out more than 1 line at once?

Skip Montanaro skip at pobox.com
Tue Nov 30 19:49:50 CET 2004


    >> if False:
    >> 
    >> (indented) block of code here is commented out

    >> I've no idea how efficient it is compared to triple-quoted strings or
    >> line by line comments.

    Gustavo> Actually, it's infinitly [sp?] more defficient (contrary of
    Gustavo> efficient?) than triple-quoted strings or line-by-line
    Gustavo> comments, because those two never make it to execution stage,
    Gustavo> because they're dropped by the compiler. :-)

If you use 'if 0:' the 2.4 byte code compiler also deletes it:

    >>> import dis
    >>> def f(a):
    ...   if False:
    ...     return a
    ...   return a+1
    ... 
    >>> dis.dis(f)
      2           0 LOAD_GLOBAL              0 (False)
                  3 JUMP_IF_FALSE            8 (to 14)
                  6 POP_TOP             

      3           7 LOAD_FAST                0 (a)
                 10 RETURN_VALUE        
                 11 JUMP_FORWARD             1 (to 15)
            >>   14 POP_TOP             

      4     >>   15 LOAD_FAST                0 (a)
                 18 LOAD_CONST               1 (1)
                 21 BINARY_ADD          
                 22 RETURN_VALUE        
    >>> def g(a):
    ...   if 0:
    ...     return a
    ...   return a+1
    ... 
    >>> dis.dis(g)
      4           0 LOAD_FAST                0 (a)
                  3 LOAD_CONST               1 (1)
                  6 BINARY_ADD          
                  7 RETURN_VALUE        

The 'if False:' case isn't optimized because you can (today, anyway)
redefine or override False.

Skip



More information about the Python-list mailing list