decorators only when __debug__ == True

LX lxkain at gmail.com
Mon Mar 29 20:54:26 EDT 2010


Hi all, I have a question about decorators. I would like to use them
for argument checking, and pre/post conditions. However, I don't want
the additional overhead when I run in non-debug mode. I could do
something like this, using a simple trace example.


@decorator
def pass_decorator(f, *args, **kw): # what about the slow-down that
incurs when using pass_decorator?
    return f(*args, **kw)

@decorator
def trace_decorator(f, *args, **kw):
    print "calling %s with args %s, %s" % (f.func_name, args, kw)
    return f(*args, **kw)

trace_enable_flag = False #__debug__
trace = trace_decorator if trace_enable_flag else pass_decorator


Trouble is, there is still an additional encapsulating function call.
Is there any way to eliminate the extra function call altogether?
Thanks in advance!



More information about the Python-list mailing list