Implementing #define macros similar to C on python

Chris Angelico rosuav at gmail.com
Thu Nov 14 22:49:52 EST 2013


On Fri, Nov 15, 2013 at 1:29 PM, JL <lightaiyee at gmail.com> wrote:
> One of my favorite tools in C/C++ language is the preprocessor macros.
>
> One example is switching certain print messages for debugging use only
>
> #ifdef DEBUG_ENABLE
> DEBUG_PRINT   print
> #else
> DEBUG_PRINT
>
> Is it possible to implement something similar in python? Thank you.

There are usually other ways to do things. For instance, you can
define a function to either do something or do nothing:

if debug_mode:
    debug_print = print
else:
    debug_print = lambda: None

debug_print("This won't be shown unless we're in debug mode!")

But as Dave says, you could write a preprocessor if you need one.

ChrisA



More information about the Python-list mailing list