any macro-like construct/technique/trick?

Neal Norwitz nnorwitz at gmail.com
Thu Jun 2 09:46:47 EDT 2005


Andrew's approach is good, but you could so something a little
simpler/more flexible.  Untested of course. :-)

Every callable object is followed by the args to pass it.  So this:
    debug_emit(DbgObjFoo(a, b, costly_function(c)))

becomes:
    debug_emit(DbgObjFoo, (a, b, costly_function, (c,)))

def debug_emit(*args):
    if not debug:
        return

    # assume last arg is not a callable and skip it
    i = len(args) - 2

    while i > 0:
        if callable(args[i]):
            # call it!  assume the next arg is a tuple of params
            new_value = args[i](*args[i+1])
            args = args[:i] + (new_value,) + args[i+2:]

    emit_dbg_code(*args)

cheers,
n




More information about the Python-list mailing list