I love the decorator in Python!!!

Ethan Furman ethan at stoneleaf.us
Thu Dec 8 18:56:58 EST 2011


Chris Angelico wrote:
> One piece of sophistication that I would rather like to see, but don't
> know how to do. Instead of *args,**kwargs, is it possible to somehow
> copy in the function's actual signature? I was testing this out in
> IDLE, and the fly help for the function no longer gave useful info
> about its argument list.

And here it is with Michele's decorator module:

2.x code (probably the same in 3.x, but haven't tested)
----------------------------------------------------------------
from decorator import decorator

debugmode = True

def trace(func):
     if debugmode:
         @decorator
         def traced(func, *a, **ka):
             print(">", func.__name__, a, ka)
             result = func(*a, **ka)
             print("<", func.__name__)
             return result
         return traced(func)
     return func

@trace
def test(x):
     "a simple test"
     print("Test! "+x)
     return 5
----------------------------------------------------------------

~Ethan~



More information about the Python-list mailing list