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/299488.html below:

Generators and Decorators doing my head in ..

Generators and Decorators doing my head in ..Paul McGuire ptmcg at austin.rr.com
Tue Sep 6 11:52:20 EDT 2005
Compare this to your original:

def logFunctionCalls(function):
    ec = FunctionCounter()
    def decoratedFunction(*args,**kwargs):
        print "Entering function:", function.__name__, ec.next()
        function(*args,**kwargs)
    return decoratedFunction

@logFunctionCalls
def doWork():
    print "Doing Work"

doWork()
doWork()

(This is a quick-and-dirty example, but it works.  A proper iterator
would do more to preserve the identity, docstring, etc. of the
underlying doWork() function.  See the decorator library page on the
Python Wiki, at http://wiki.python.org/moin/PythonDecoratorLibrary.)

Also, look into the treasure trove that is itertools, especially
itertools.count.

-- Paul


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