A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/333410.html below:

Generators and Decorators doing my head in ..

Generators and Decorators doing my head in .. 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4