faqts : Computers : Programming : Languages : Python

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

I want an equivalent of c "#define DEBUG 1" for my file that if set print a debug message....

Feb 14th, 2007 10:42
stephen brown, Nick Lunghi,


The short answer is, there is no equivalent, because Python is an
interpreted language and there is no explicit compilation step, hence no
distinction between "compile time" and "run time".

However, there are several ways to get similar functionality.  You can
simply define a variable:

Debug=False
print 'Do something.'
if Debug:
    print 'Debugging message.'

Then you can turn debugging on and off by editing one line of code. 
This is no more onerous than re-compiling, but it does require
changing the source, however slightly, which may not be satisfactory. 
For code spread across several modules, it may not be very convenient. 
You'd need to have the namespace in which Debug exists accessible.  One
way to do this would be to have a Debug module, i.e. in the file Debug.py:

Debug=1 # change to 0 or False to turn off debugging

and then in the modules you wanted to use the flag, include the line

from Debug import Debug

Then changing a single line in the Debug module will affect the
performance of all the modules that import it.  Of course, the Debug
module could also include useful methods.

If you want to ship code including the debug statements, but don't wish
to ship the Debug module, you should check whether it's there:

try:
    from Debug import Debug
except ImportError:
    Debug = False

From here, it's obvious to get to:

try:
    import Debug
    Debug = True
except ImportError:
    Debug = False

and now the file Debug.py doesn't even need to have any content, just
it's presence or absence will switch the Debug flag.  But there's a
simpler way to do that:

import os.path
Debug = os.path.exists('Debug.py')

But why have a file at all, when there are other mechanisms?

A traditional way (not only in Python) to affect the run-time behaviour
of programs is with environment variables.  These are easy in Python:

import os
Debug = os.getenv('Debug')
print 'Do something.'
if Debug:
    print 'Debugging message.'

os.getenv() returns None for environment variables that are not set, so
this has appropriate default behaviour.  The Python implementation is
portable, but the operating system commands to set environment variables
differ across systems.  Now no changes to any source code are required.

Other options to get this kind of functionality are to use the built-in
'user' module to run some initialization code, to use the 'logging'
module, or to use the 'warnings' module.  Note there is a built-in
variable __debug__, whose value is False if the -O option is to invoke
the interpreter--you could pervert this mechanism to do something like
what you want.  But I think a Debug module pretty straightforward.