debugging code
Rick Ratzel
rick.ratzel at magma-da.com
Tue Apr 27 18:57:04 EDT 2004
Python defines __debug__ to True by default, and False when
optimizations are enabled...meaning you can enable/disable code without
having to define/undefine vars ahead of time and without having to
change it prior to deployment. This is how the "assert" statement
works. You can only set __debug__ through the use of -O or -OO.
For example, you can define a func like this:
def printDebugStmt( stmt ) :
if __debug__ : print stmt
...and use it like this:
$ python
>>> i=3
>>> printDebugStmt( "Hello World...i = %s" % i )
Hello World...i = 3
>>>
$ python -O
>>> i=3
>>> printDebugStmt( "Hello World...i = %s" % i )
>>>
...just remember to "ship" your app so it runs Python with -O or -OO.
BTW: you can use freeze.py with -O or -OO to get the same result for a
"frozen" app.
-Rick
beliavsky at aol.com wrote:
> If I have some debugging code in a Python program (mostly print
> statements) and I want to turn it "off", two obvious methods are to
> (1) comment out the code
> (2) set a logical 'debug' variable at the beginning and use it to
> conditionally run the debugging code.
>
> Is there a better way? Some other languages have conditional
> compilation. (I don't know if that is better or worse). What is the
> "best" way to maintain "production" and "debugging" versions of a
> Python program at the same time, preferably in the same file?
More information about the Python-list
mailing list