any macro-like construct/technique/trick?



Is there a way to mimic the behaviour of C/C++'s preprocessor for
macros? The problem: a lot of code like this:

def foo():
# .... do some stuff
if debug:
emit_dbg_obj(DbgObjFoo(a,b,c))

# .... do more stuff
if debug:
emit_dbg_obj(DbgObjBar(d,e))

# ... and so on ...

Notes:

* the two-lines of debug conditional tend to really break up the flow
of the surrounding code

* in C you could wrap them with a macro so you could do
DEBUG_EMIT(DbgObjFoo(a,b,c)), etc, with the macro only instantiating
the object and processing it if the debug flag was set. The one-liner
is MUCH less disruptive visually when reading code

* 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

.



Relevant Pages

  • Re: define_method and instance variable maddness
    ... use 'define_method', but then their methods cease to take blocks, ... enclose scope which cannot be freed, and are a pain in the butt to ... def add_personnel ... it's faster to debug by light years since you can dump ...
    (comp.lang.ruby)
  • Re: define_method and instance variable maddness
    ... def add_personnel ... it's faster to debug by light years since you can dump ...
    (comp.lang.ruby)
  • Re: how to export this function?
    ... Both are debug version. ... but when I use __declspec__cdecl CString ... GetStringCodewithout .def file, after compile, ... the Dependency shows the exported function is ...
    (microsoft.public.vc.language)
  • Re: any macro-like construct/technique/trick?
    ... > def foo(): ... > if debug: ... > DEBUG_EMIT), etc, with the macro only instantiating ... def call_if_debug: ...
    (comp.lang.python)
  • Re: Optimizing constants in loops
    ... def func: ... Could the "if debug" be optimized away on function invocation if debug ... The optimizer works in the way you describe above (which ...
    (comp.lang.python)