"Mac" <[EMAIL PROTECTED]> wrote: > Is there a way to mimic the behaviour of C/C++'s preprocessor for > macros? > [...] > > * using > def debug_emit(obj): > if debug: > emit_dbg_obj(obj) > is a poor solution, because it *always* instantiates DbgObj*, even when > not needed; I want to avoid such unnecessary waste
How about using assert statements? When you run with optimization turned on, they don't even get compiled. Try something like this: class Debug: def __init__ (self, message): print "debug (%s)" % message print "foo" assert (Debug ("bar")) which produces: Roy-Smiths-Computer:play$ python x.py foo debug (bar) Roy-Smiths-Computer:play$ python -O x.py foo Notice that the Debug() object doesn't even get created when optimization is turned on. It's a little funky, but at least anybody who knows python will understand your code. I agree with Andrew Dalke when he says of preprocessors: > It's typically a bad idea because you're in essence creating a > new language that is similar to but not Python, making it harder > for people to understand what's going on. -- http://mail.python.org/mailman/listinfo/python-list