Using debug print routine inside assert

Alex Martelli aleax at aleax.it
Wed Nov 5 06:00:20 EST 2003


Edvard Majakari wrote:

> Alex Martelli <aleax at aleax.it> writes:
> 
>> def debug(msg, *args):
>>     if not must_do_some_output: return
>>
>>     if args:
>>         msg = msg % args[0](*args[1:])
>>
>>     # proceed to output msg
>>
>> and called as
> 
> Gee - I'm impressed to receive answer to my original problem from
> Martellibot himself :)

<bow> :-)


> This is an excellent solution. I just realised it is backwards compatible
> with my current system - calling debug('foo bar baf %s' %method_call())
> works just fine with Alex's version; it just works the same as before, and
> doesn't get any advantage from deferred parameter list evaluation.

Yes, the deferred evaluation above is just optional.


> Of course there's still minor overhead in calling the method instead of
> completely skipping it like with assert(), but usually that doesn't matter
> at all.

Usually not.  When it does (you've got lots of calls to debug that you'd
rather not just comment out and the overhead even just for debug checking
a flag and returning is annoying) the fastest approach is

debug = noop.noop

where noop is a do-nothing C-coded callable that accepts any args at all
and returns None... (also make sure the name 'debug' is local to the
function doing the calls, else the name lookup overhead is being paid:-)


Alex






More information about the Python-list mailing list