0

I print out (m x n) table of values for debugging, however, I do not want the debug messages to be printed out in non-debugging mode. In C, it can be done with "#ifdef _DEBUG" in code and define _DEBUG in preprocessor definition. May I know what is equivalent way in Python?

twfx
  • 1,664
  • 9
  • 29
  • 56

3 Answers3

3

Python has module called "logging" See this question: Using print statements only to debug

Or the basic tutorial: http://docs.python.org/2/howto/logging.html

Community
  • 1
  • 1
Gajo
  • 66
  • 4
0

You could define a global variable someplace, if that's what you want. However, probably the cleaner and more standard way is to read a config file (easy because you can write a config file in plain Python) and define DEBUG in there. So you've got a config file that looks like this:

# program.cfg
# Other comments
# And maybe other configuration settings
DEBUG = True # Or False

And then in your code, you can either import your config file (if it's in a directory on the Python path and has a Python extension), or else you can execfile it.

cfg = {}
execfile('program.cfg', cfg) # Execute the config file in the new "cfg" namespace.
print cfg.get('DEBUG') # Access configuration settings like this.
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
0

try this:

import settings
if settings.DEBUG:
    print testval

This prints testval if, and only if, DEBUG=True in settings.py

AMADANON Inc.
  • 5,753
  • 21
  • 31