Generators and Decorators doing my head in ..

simonvc at gmail.com simonvc at gmail.com
Tue Sep 6 11:35:59 EDT 2005


Hi Can someone please help me understand this (i shouldn't have tried
to learn decorators and generators at the same time..).

Im trying to create a decorator that counts the number of times a
function is run.  Somthing like:

def FunctionCounter():
    n=0
    while 1:
        yield n
        n=n+1

def logFunctionCalls(function):
    print "Entering function:", function.__name__, ec.next()
    return function

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


doWork()

Where "ec" is a generator that is created inside logFunctionCalls if it
doesnt already exist.
Im thinking this has somthing to do with anonymous generators, but im
not sure how you create or access these..

Apologies in advance if this is moronic.




More information about the Python-list mailing list