idiom for debug code? [1/1]

Steve Holden steve at holdenweb.com
Sun Oct 3 04:31:02 CEST 2004


Ian Parker wrote:

[...]
> I have a utility.py module which contains my common functions and I
> import this in every module, plus a wrapper for the debug function:
> 
> import utility
> 
> debug = 0
> 
> def dprint( *args):
>     """
>         print function toggled on or off by module debug variable
>     """
>     global debug
>     if debug:
>         apply(utility._dprint,args)
> 
> 
> This allows me to scatter dprint functions throughout the module instead
> of print statements, and enable or suppress them by setting the global
> 'debug', e.g.
> 
>     def func(x):
>         global total
>         total += x
>         dprint("total is currently",total)
> 
> to get an output of something like:
>     ! module.py:83 in func: total is currently 10
> 

Well, I don't do anything as sophisticated as dprint on the output side, 
and might consider lifting it. But in terms of selecting which debug 
code gets executed I often use something like

	if 3 in debug:

because this makes it easy to selectively turn different bits of debug 
code on or off. Of course the test takes a little longer, but when not 
debugging it doesn't take long to check whether an integer is in an 
empty list.

regards
  Steve



More information about the Python-list mailing list