Using debug print routine inside assert

Richard Brodie R.Brodie at
Tue Nov 4 15:22:32 CET 2003

"Edvard Majakari" <edvard+news at> wrote in message
news:8765i072jh.fsf at

> 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):

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:

More information about the Python-list mailing list