Using debug print routine inside assert

Richard Brodie R.Brodie at rl.ac.uk
Tue Nov 4 09:22:32 EST 2003


"Edvard Majakari" <edvard+news at majakari.net> wrote in message
news:8765i072jh.fsf at titan.staselog.com...

> Today I shortly discussed the problems in using print statements for
> debugging problems in Python. Often in coding, in addition to asserts it
> is nice to have a debug routine like

> # somewhere in the program
> debug("subfrobnicate(%s, %s) returned %s" %(p1, p2, subfrobnicate(p1, p2)))
>
> The idea here is to have consistent debug print messages. However,
> parameters or expressions inside the debug statement might be very
> time-consuming to run.

I'm not sure how often you would need to write:

debug("subfrobnicate(%s, %s) returned %s" %(p1, p2, subfrobnicate(p1, p2)))

without having called subfrobnicate in the main code. Assuming that you do,
though, it's relatively easy to juggle around the code by having something like:

def defer(f, *args):
        f(*args)

and writing:

 debug("subfrobnicate(%s, %s) returned %s" % (p1, p2, defer(subfrobnicate, p1, p2)))

Also note that Python now has a standard logging module since 2.3:
http://www.python.org/doc/2.3/whatsnew/







More information about the Python-list mailing list