Python has a few ways of printing "trace" output. print
, import logging
, stdout.write
can be used to print debugging info, but they all have one drawback: even if the logger's threshold is too high or the stream is closed, Python will still evaluate the arguments to the print statement. (Strict Evaluation) This could cost a string format or more.
The obvious fix is to put the string-creating code into a lambda, and use our own logging function to call the lambda conditionally (this one checks the __debug__
builtin variable, which is set to False whenever python is started with -O
for optimizations) :
def debug(f):
if __debug__:
print f()
#stdout.write(f())
#logging.debug(f())
for currentItem in allItems:
debug(lambda:"Working on {0}".format(currentItem))
The advantage is not calling str(currentItem)
and string.format
in release builds, and the disadvantage is having to type in lambda:
on every logging statement.
Python's assert
statement is treated specially by the Python compiler. If python is run with -O
, then any assert statements are discarded without any evaluation. You can exploit this to make another conditionally-evaluated logging statement:
assert(logging.debug("Working on {0}".format(currentItem)) or True)
This line will not be evaluated when Python is started with -O
.
The short-circuit operators 'and' and 'or' can even be used:
__debug__ and logging.debug("Working on {0}".format(currentItem));
But now we're up to 28 characters plus the code for the output string.
The question I'm getting to: Are there any standard python statements or functions that have the same conditional-evaluation properties as the assert
statement? Or, does anyone have any alternatives to the methods presented here?
The Python debugger is an interactive source code debugger for Python programs. It can set conditional breakpoints and single stepping at the source line level. It also supports inspection of stack frames, source code listing, and evaluation of arbitrary Python code in any stack frame's context.
A debugger is a program that can help you find out what is going on in a computer program. You can stop the execution at any prescribed line number, print out variables, continue execution, stop again, execute statements one by one, and repeat such actions until you have tracked down abnormal behavior and found bugs.
Python if statement evaluates a boolean expression to true or false, if the condition is true then the statement inside the if block will be executed in case if the condition is false then the statement present inside the else block will be executed only if you have written the else block otherwise it will do nothing.
If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.
if all your debug function will take is a string, why not change it to take a format string and the arguments:
debug(lambda:"Working on {0}".format(currentItem))
becomes
debug("Working on {0}", currentItem)
and
if __debug__:
def debug(format, *values):
print format.format(*values)
else:
def debug(format, *values): pass
this has all the advantages of your first option without the need for a lambda and if the if __debug__:
is moved out the of function so that it is only tested once when the containing module is loaded the overhead of the statement is just one function call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With