Generators and Decorators doing my head in ..

Michele Simionato michele.simionato at gmail.com
Wed Sep 7 04:51:37 EDT 2005


I usually point out my decorator module
(http://www.phyast.pitt.edu/~micheles/python/decorator.zip) to simplify
decorator usage. In this case you would use it as follows:

from decorator import decorator

@decorator # convert logFunctionCalls into a decorator
def logFunctionCalls(function, *args, **kwargs):
    try: # increment the counter
        function.counter += 1
    except AttributeError: # first call, there is no counter attribute
        function.counter = 1
    print "Entering function:", function.__name__, function.counter
    return function(*args, **kwargs)

@logFunctionCalls
def f():
    pass

f()
f()
f()

help(f)

The whole point of the decorator module is that the signature of
the original function is left unchanged (i.e. in this case the
decorated f is still a thunk, not a generic function f(*args, **kw)).
HTH,

             Michele Simionato




More information about the Python-list mailing list