idiom for debug code? [1/1]

Ian Parker parker at hiredatum.demon.co.uk
Sun Oct 3 05:36:20 EDT 2004


In message <HzJ7d.288401$4o.8905 at fed1read01>, Steve Holden 
<steve at holdenweb.com> writes
>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

I like that - it does give a flexibility that my dprint lacks.  I do 
tend to find that, although I may enable my dprint only for a function 
or method,  I still comment out some of the dprint calls.

Regards

Ian

-- 
Ian Parker



More information about the Python-list mailing list