Search code examples
pythondebuggingcompilation

Checking for __debug__ and some other condition in Python


In Python sometimes I want to do something like (1)

if __debug__ and verbose: print "whatever"

If Python is run with -O, then I'd like for that whole piece of code to disappear, as it would if I just had (2)

if __debug__: print "whatever"

or even (3)

if __debug__:
    if verbose: print foo

However, that doesn't seem to happen (see below). Is there a way I can get the run-time efficiency of #3 with compact code more like #1?

Here's how I tested that I'm not getting the efficient code I want:

#!/usr/bin/python2.7

from dis import dis
import sys

cmds = ["""
def func ():
    if __debug__ and 1+1: sys.stdout.write('spam')""",   """
def func():
    if __debug__: sys.stdout.write('ham')""",     """
def func():
    __debug__ and sys.stdout.write('eggs')"""]

print "__debug__ is", __debug__, "\n\n\n"

for cmd in cmds:
    print "*"*80, "\nSource of {}\n\ncompiles to:".format(cmd)
    exec(cmd)
    dis(func)
    print "\n"*4

Running this gives

__debug__ is False 



******************************************************************************** 
Source of 
def func ():
    if __debug__ and 1+1: sys.stdout.write('spam')

compiles to:
  3           0 LOAD_GLOBAL              0 (__debug__)
              3 POP_JUMP_IF_FALSE       31
              6 LOAD_CONST               3 (2)
              9 POP_JUMP_IF_FALSE       31
             12 LOAD_GLOBAL              1 (sys)
             15 LOAD_ATTR                2 (stdout)
             18 LOAD_ATTR                3 (write)
             21 LOAD_CONST               2 ('spam')
             24 CALL_FUNCTION            1
             27 POP_TOP             
             28 JUMP_FORWARD             0 (to 31)
        >>   31 LOAD_CONST               0 (None)
             34 RETURN_VALUE        





******************************************************************************** 
Source of 
def func():
    if __debug__: sys.stdout.write('ham')

compiles to:
  3           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE        





******************************************************************************** 
Source of 
def func():
    __debug__ and sys.stdout.write('eggs')

compiles to:
  3           0 LOAD_GLOBAL              0 (__debug__)
              3 JUMP_IF_FALSE_OR_POP    21
              6 LOAD_GLOBAL              1 (sys)
              9 LOAD_ATTR                2 (stdout)
             12 LOAD_ATTR                3 (write)
             15 LOAD_CONST               1 ('eggs')
             18 CALL_FUNCTION            1
        >>   21 POP_TOP             
             22 LOAD_CONST               0 (None)
             25 RETURN_VALUE        

Solution

  • No, you can't. Python's compiler is not nearly smart enough to detect in what cases it could remove the code block and if statement.

    Python would have to do a whole lot of logic inference otherwise. Compare:

    if __debug__ or verbose:
    

    with

    if __debug__ and verbose:
    

    for example. Python would have to detect the difference between these two expressions at compile time; one can be optimised away, the other cannot.

    Note that the difference in runtime between code with and without if __debug__ statements is truly minute, everything else being equal. A small constant value test and jump is not anything to fuss about, really.