4

I have a huge python code with lots of print statements useful for debugging. I want to be able to enable or disable them in one go, without poring over the hundreds of printf's and commenting them each time.

In C, a #define can be used to comment out unneeded parts of the code using #ifdef like this-

#define debug
#ifdef debug
    printf("Debug on")
#endif

If I don't want to be in debug mode, I can simply comment #define debug and none of my print statements will compile.

How can this functionality be done in Python?

Matthew Turner
  • 3,564
  • 2
  • 20
  • 21
sbhatla
  • 1,040
  • 2
  • 22
  • 34
  • Since C macros are evaluated at compile time to simplify the runtime, and since Python does not have this separation, such a functionality doesn’t make much sense in Python. You can just use normal run-time checks to make branches (read: simple ifs). – poke Nov 21 '14 at 21:47
  • Also related: http://stackoverflow.com/questions/482014/how-would-you-do-the-equivalent-of-preprocessor-directives-in-python – John Y Nov 21 '14 at 22:54

3 Answers3

4

Python has no direct equivalent of C's macros because it has no preprocessor and does not distinguish between compile-time and run-time like C does.

A simple solution however is to put your print lines inside an if-statement:

if False:
    print(...)
    print(...)
    print(...)
    ...

You can then just change the False to True to have them be executed.


Similarly, you could do:

DEBUG = False

if DEBUG:
    print(...)
    print(...)
    print(...)
    ...

and then change the DEBUG name to True.


A third (and probably the best) option would be to use Python's built-in __debug__ flag:

if __debug__:
    print(...)
    print(...)
    print(...)
    ...

__debug__ is a constant like None and is set to True if Python is launched without a -O option (it is in debug mode). Otherwise, if a -O option is set (we are in optimized/production mode), __debug__ will be set to False and the code using it will be entirely ignored by the interpreter so that there is no performance penalty.

  • 1
    Use `if __debug__`. This flag is set by the `-O` option and it actually *strips out* code when running in production (optimized) mode, so there is no performance penalty. – kindall Nov 21 '14 at 23:37
  • Thanks, I forgot about `__debug__`. –  Nov 21 '14 at 23:55
2

You should look into the Python logging library:

https://docs.python.org/2/library/logging.html

igon
  • 3,016
  • 1
  • 22
  • 37
1

Use the python standard library logging module. Here is an easier-to-read tutorial on the logging module.

You can set the debugging level for a given run of your code like so (see this section for more info):

import logging
# if you want you can also set filename="logfile.txt" to write to file
logging.basicConfig(level=logging.DEBUG) # there are other options like logging.INFO
x = 42.31415
# Run your program here....
logging.debug("x is now" + str(x))

So you could even pass the debug level at the command line if you so choose, say doing something like

--loggingLevel=DEBUG
Matthew Turner
  • 3,564
  • 2
  • 20
  • 21